### Code always defaults to first "else if" in my if else statement.

Just to practice I wrote up a basic GPA calculator, but for some reason the code only prints out the first "else if" statement... but it always gets the grade right, so i know its working.. just not working right.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` `````` #include using namespace std; int main(void){ double math = 0.0; double science = 0.0; double history = 0.0; double english = 0.0; double sum = 0.0; double average = 0.0; cout << "Welcome to the grade average calculator!"<> math; cout << "Pleas enter your Science grade:"; //For science grade cin >> science; cout << "Pleas enter your History grade:"; //For history grade cin >> history; cout << "Pleas enter your English grade:"; //For english grade cin >> english; sum = (math + science + history + english); //Math + Science + History + English average = (sum / 4); /* ALL THE IF STATEMENTS ARE BELOW THIS POINT AND WILL BE MARKED BY GRADE */ if (average == 100){ cout << "Wow! Your doing great! You have a " << average; } //For 100 GPA else if (average > 89 || average < 100){ cout << "Your doing great! Your GPA is a " << average; } //For 90 to 99 GPA else if (average > 79 || average < 90 ){ cout << "Doing pretty good! Your GPA is " << average; } //For 80 to 89 GPA else if (average > 74 || average < 80){ cout << "Your doing fine. Your average is " << average; } //For 75 to 79 GPA else if (average > 64 || average < 75){ cout << "You're not doing too hot... You have a " << average; } //For 65 to 75 GPA else if (average == 0){ cout << "You either didnt show up, or your a failure... Your average is " << average; } //For 0 GPA lol... else if (average > 100 || average < 0){ cout << "ERROR: You cannot have a GPA higher then 100 or lower then 0, and yours was apparently " << average; } //Error for GPA higher then 100 or lower then 0 else { cout << "Uh-oh! Your failing with a " << average; } //For everything els (Failing grades) return 0; }``````
 `` `` ``else if (average > 89 || average < 100)``

Need to be:
 `` `` ``else if (average > 89 && average < 100)``

Your logic right now is if the grade is greater than 89 OR less than 100, which will always evaluate to true. So use && instead of || for your else ifs.
Thanks, and im sorry. Im brand new with this code language... Im switching up from C which i bearly knew in the first place. Thank you very much.
It's no problem and don't feel bad, that's actually a very common mistake people tend to make.
Comparing floating point values is a bad idea in most cases. Because of how computers store data even if you do 400.0 / 4 there is a (high) chance that the result will not be exactly 100, but rather something like 99.99999999987 (different from 100).
Last edited on
Well how do i fix something like that?
You can allow some degree of error with something like this
 ``12`` ``````const double ERROR = 0.000005; if(average >= 100 - ERROR && average <= 100 + ERROR)``````

But I'm not sure if it's the only viable option. When I have to deal with floats I just avoid equality comparisons.
On a side note, 0 can be exactly represented, so a comparison with 0 is "safer".
This is somewhat related... and I'm going to throw it out there because I see this problem a lot and it's a really, really bad habit.

You are duplicating code in the form of doubling your conditions. For example this bit here:

 ``1234`` ``````// ...Assuming the maximum value for 'average' is 100: if (average == 100){} else if (average > 89 || average < 100){}``````

`else` by definition will only execute if the previous `if` condition was false. Therefore the else here already ensures that average is less than 100. You do not (and should not) check for that again.

Something like this would be preferable:

 ``123`` ``````if(average >= 100) { } // <- avoid using == or != with floating points, as has been mentioned else if(average >= 90) { } // <- no need to see if average is less than 100... already know it is else if(average >= 80) { } // <- ditto ``````

Aside from avoiding the confusion resulting with &&/|| mixups (which was your original problem), you remove code duplication which makes the code easier to maintain and less mistake-prone.
Topic archived. No new replies allowed.