Thread: Simple C++ question. Error in code somewhere.

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Question Simple C++ question. Error in code somewhere.

    Hello. At school in Computing Studies I'm doing programming and the language that I have to use there is True BASIC. I have made a simple program in TB but want to try making it in C++ instead. By looking at the code you will probably see how simple the program I'm making is but since I've been working on simple C++ programs for only a few weeks I'm not quite sure what I'm doing wrong. You will probably want to see the code I've done so far:

    Code:
    //Made by <Removed following Request -- Fordy>
    //04/02/06
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int total;
        int score;
        int percent;
    
        cout<<"What is the total score? ";
        cin>> total;
        cin.ignore();
        cout<<"What is your score? ";
        cin>> score;
        cin.ignore();
        percent = ( score / total ) * 100;
        cout<<"Your percentage mark is: "<< percent <<"\n";
        cin.get();
    }
    CORRECTED THE CODE.

    The area what I've marked in bold is where the error is or at least thats what the Dev-C++ tells me. I'm not sure if I'm doing the calculation correct or if anything in that line is. If you think you can help me then please reply.

    Thanks in advance.
    Last edited by Paracropolis; 02-06-2006 at 04:38 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Remove the
    cin >>
    from the start of the line.

    Then work out what the difference between integer and float division is...

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Quite a silly error I made there but thanks for your help.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Does anyone know why the percentage total always comes to zero?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, for the reason stated in my previous post.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    There are two types of division in C++: integer division and float division. You need to determine the differences between the two types of division and which one is operating in your program.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    I have read some articles about this integer and float division but I don't understand how I'm to incorporate the this into my code. Sorry if I'm asking for too much help.

    If you think it would be better to talk about this on MSN then add me to your contact list: [email protected]
    Last edited by Paracropolis; 02-06-2006 at 04:41 AM.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    you could try this and you won't need floats (unless you got .5 in a score):

    #include <iostream>

    Code:
    using namespace std;
    
    int main()
    {
        int total;
        int score;
        int percent;
    
        cout<<"What is the total score? ";
        cin>> total;
    
        cout<<"What is your score? ";
        cin>> score;
    
        percent = ( score*100 ) / total;
        cout<<"Your percentage mark is: "<< percent <<"\n";
    
    	return 0;
    }

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Thanks for you help on MSN!

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I have read some articles about this integer and float division but I don't understand how I'm to incorporate the this into my code.
    Integer and floating point division happen automatically, depending on the variable types or values invovled, and the result will be different. So you need to figure out which one is operating in your program. You can change whether integer or floating point division occurs by changing the type of the variables and values used in your calculations.
    Last edited by 7stud; 02-06-2006 at 05:53 AM.

  11. #11
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    i thik it s ok

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  2. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 01:59 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Large code Simple Question need help
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 10-25-2002, 07:55 PM
  5. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 01:33 PM