Thread: If/else if/else problem

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    16

    If/else if/else problem

    Ok so I am not seeing why my code is not working. The only way I can get the A resposne is if I input 1000 points or higher. If anything less that 1000 total points are put in the program jumps to the else statment unless the exampoints are less than 240, at least that part of the program works. Also I would love to be able to debug this myself but I can not figure out how to do it with jgrasp, i think the issue is because I am using borland 5.5 compiler. Anyways here is the program. Any help would be appriciated.
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    const int POSSIBLE = 1000;
    const int A = 90;
    const int B = 80;
    const int C = 70;
    const int D = 60;
    const int EXAM = 240;
    int main()
    {
       int exam;
       int hw;
       int assignment;
       int earned;
       int grade;
       
       cout << "How many points did you earn on exams?" << endl;
       cin >> exam;
       cout << "How many points did you earn on the MPL homework?" << endl;
       cin >> hw;
       cout << "How many points did you earn on the program assignments?" << endl;
       cin >> assignment;
       
       earned = exam + hw + assignment;
       
       grade = (earned / POSSIBLE) * 100;
       
       if (grade >= A && exam >= EXAM)
          {
                cout << "You earned a total of" << " " << earned << " " << "points" << endl;
                cout << "Congratulations this gives you a letter grade of A!" << endl;
          }
       else if (grade >= B && exam >= EXAM)
             {
                   cout << "You earned a total of" << " " << earned << " " << "points" << endl;
                   cout << "Congratulations this gives you a letter grade of B!" << endl;
             }
       else if (grade >= C && exam >= EXAM)
          {
                cout << "You earned a total of" << " " << earned << " " << "points" << endl;
                cout << "Good job this gives you a letter grade of C!" << endl;        
          }
       else if (grade >= D && exam >= EXAM)
          {
                cout << "You earned a total of" << " " << earned << "points" << endl;
                cout << "Well at least you passed with a letter grade of D!" << endl;       
          }
      
       else if (exam < EXAM)
          {  
             cout << "You earned a total of" << " " << earned << "points" << endl;
             cout << "You got a grade of F and have failed this class" << endl; 
             cout << "If you did not get at least 240 points on the exams" << endl;
             cout << "you have failed because to pass the class you must" << endl;
             cout << "have at least 240 points on exams in order to pass" << endl;
          }
       else 
          {
             cout << "Error you have entered a negative vaule for points earned." << endl;
             cout << "in this class" << endl;
             cout << "Please run program again and enter positive value" << endl;
             cout << "for points earned in the course" << endl;
          }    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look-up Integer division.

    Code:
    grade = (earned / POSSIBLE) * 100;
    Example 0 = 3/4

    I would suggest this

    Code:
    grade = ((double)earned / POSSIBLE) * 100;
    or this

    Code:
    grade = (100.0 * earned) / POSSIBLE;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Every integer division produces an integral result with rounding toward zero. So earned/POSSIBLE will always produce a result of zero if earned is less than 1000.

    Instead of "grade = (learned/POSSIBLE)*100" try doing "(100*learned)/POSSIBLE". The division by POSSIBLE (i.e. 1000) still rounds toward zero, but 100*learned is a bigger value, so only values of learned less than 10 will produce a result of zero.

    The compiler is not permitted to reorder operations on integral values/types to give a result you might expect (as you are in this case) - because the standard describes rules of precedence and associativity of operators that are fixed. That means YOU have to reorder operations to give the result you expect.

    I wouldn't suggest floating point (like Tim describes) unless there is a particular need for floating point in subsequent code - which is not the case here.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    16
    thank you so much I have been trying to figure this out for like 5 hours across two days and could not figure out why it wasn't working.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    16
    Now that I think of it, that makes sense and I know that with int it takes anything less than the whole number and rounds down but I kept doing the math and thinking it works here why doesnt it work in the code not thinking of how integers work in c++.

  6. #6
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    By the way, at the moment you're declaring all variables used in your main function at the top of the function. Don't do that. That isn't the C++ way, in fact it's not even the modern C way anymore. You want to declare your local variables as close to their first use as possible, thereby limiting them to the smallest scope possible. That way you can also assign them a proper value right away ... instead of leaving them uninitialized until you (hopefully) remember to assign them a value later in the program. Now with int exam;, int hw; and int assignment you don't have much of a choice, but earned and grade could have been declared and initialized in one step (and made const to boot!).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  4. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM