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.
#include <iostream>
usingnamespace 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!"<<endl;
cout << "Pleas enter your Math grade:"; //For math grade
cin >> 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
elseif (average > 89 || average < 100){
cout << "Your doing great! Your GPA is a " << average;
}
//For 90 to 99 GPA
elseif (average > 79 || average < 90 ){
cout << "Doing pretty good! Your GPA is " << average;
}
//For 80 to 89 GPA
elseif (average > 74 || average < 80){
cout << "Your doing fine. Your average is " << average;
}
//For 75 to 79 GPA
elseif (average > 64 || average < 75){
cout << "You're not doing too hot... You have a " << average;
}
//For 65 to 75 GPA
elseif (average == 0){
cout << "You either didnt show up, or your a failure... Your average is " << average;
}
//For 0 GPA lol...
elseif (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;
}
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.
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).
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:
1 2 3 4
// ...Assuming the maximum value for 'average' is 100:
if (average == 100){}
elseif (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:
1 2 3
if(average >= 100) { } // <- avoid using == or != with floating points, as has been mentioned
elseif(average >= 90) { } // <- no need to see if average is less than 100... already know it is
elseif(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.