### Having trouble with if, else if, and else statements

I'm trying to code a program that will tell me the average and letter grade for three tests put in. I have the averaging worked out, but I can't seem to figure out how to get the correct letter grade. I'm fairly new to programming, so any help would be nice. Thanks

 ``123456789101112131415161718192021222324252627282930313233343536`` `````` // What grade? #include using namespace std; int main() { int a; // First test's grade int b; // Second test's grade int c; // Third test's grade cout << "What did you get on the first test?"; cin >> a; // User imputs first test's grade if(a == 100){ cout << "Thats a perfect grade!"; } else if(100 > a >= 90){ cout << "That's an A"; } else if(90 > a >= 80){ cout << "That's a B"; } else if(80 > a >= 70){ cout << "That's a C"; } else if(70 > a >= 65){ cout << "That's a D"; } else{ cout << "That's an F, you failed, better luck next time..." << endl; } cout << "What did you get in the second test?"; cin >> b; // User imputs second test's grade cout << "What did you get on the third test?"; cin >> c; // User imputs third test's grade int average = (a+b+c)/3; // Calculates average cout << "Your test average is :" << average << endl; // Displays average return 0; }``````

This is the output after putting in the first grade:

What did you get on the first test? 89
That's an F, you failed, better luck next time...
What did you get in the second test?
Last edited on
Try

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` `````` // What grade? #include using namespace std; int main() { int a; // First test's grade int b; // Second test's grade int c; // Third test's grade cout << "What did you get on the first test?"; cin >> a; // User imputs first test's grade if(a == 100) { cout << "Thats a perfect grade!"; } else if(a >= 90) { cout << "That's an A"; } else if(a >= 80) { cout << "That's a B"; } else if(a >= 70) { cout << "That's a C"; } else if(a >= 65) { cout << "That's a D"; } else { cout << "That's an F, you failed, better luck next time..." << endl; } cout << "What did you get in the second test?"; cin >> b; // User imputs second test's grade cout << "What did you get on the third test?"; cin >> c; // User imputs third test's grade int average = (a+b+c)/3; // Calculates average cout << "Your test average is :" << average << endl; // Displays average return 0; }``````
Thanks, it worked! If you don't mind, could you described what you changed?
`if (100 > a >= 90)`
is not doing what you want it to do. Every comparison operator must stand on its own.
What you actually meant was
`if (100 > a && a >= 90)`

SamuelAdams simplified the redundant nature of the logic by removing the first condition of each statement.
OK thanks.
Ganado is correct. In the statement, else if(90 > a >= 80), the reason I removed the "90 > a" part is because it was redundant in this case. If you were doing something highly important, you might want to include a check like you tried.

Sometimes less typing is faster but at other times it can get you into trouble.
Another way to write that condition is
if ((100 > a) && (a >= 90))
I may be wrong but personally I think it's safer and easier to debug when combining statements such as this.
Last edited on
 `` `` ``else if(100 > a >= 90)``

Here's what happens with the above code fragment:

1) First 100 > a is evaluated. (Assume a is 89). It returns a true (which is equivalent to 1.)

2) The expression now becomes:

 ``12`` ``````/// 1 is the equivalent of the true returned by the 1st sub-expression. else if(1 >= 90)``````

This evaluates to false.

3) All the if-else expressions similarly evaluate to false.

4) Hence, the else is what gets finally evaluated and an 'F' is printed.