nested if

Hello, I wrote a program that let you enter 4 tests scores and calculates them. And it suppose to give you the average, grades, and a message if you did really good or really bad. I debugged it but it only showed the average but not the grades and messages. Help me please.
#include <iostream>

using namespace std;

int main()
{
int firstTest;
int secondTest;
int thirdTest;
int forthTest;
int average;


cout << " Enter the first test score: " << endl;
cin >> firstTest;
cout << " Enter the second test score: " << endl;
cin >> secondTest;
cout << " Enter the third test score: " << endl;
cin >> thirdTest;
cout << " Enter the forth test score: " << endl;
cin >> forthTest;

average = (firstTest + secondTest + thirdTest + forthTest) / 4;

// cout << " The average of the four tests is: " << average << endl;

do
{

if (average >= 95 && <= 100)
{
cout << " Your average is: " << average << endl; "You got an A+ ! Congratualtions ! " << endl;
}
else if (average >= 90 && <= 94)
{
cout << " Your average is: " << average << endl; " You got an A ! Good job ! " << endl;
}
else if (average >= 85 && <= 89)
{
cout << " Your average is: " << average << endl; " You got a B+ ! " << endl;
}
else if (average >= 75 && <= 84)
{
cout << " Your average is: " << average << endl; " You got a C+ ! " << endl;
}
else if (average >= 69 && <= 74)
{
cout << " Your average is: " << average << endl; " You got a C- ! " << endl;
}
else if (average >= 65 && <= 68)
{
cout << " Your average is: " << average << endl; " You got a D ! " << endl;
}
else (average <= 64)
{
cout << " Your average is: " << average << endl; " You got an F, you fail the class ! " << endl;
}
}while (average >= 0 && <= 100);
cin.ignore();
cin.get();

}
Try this on for size:
if (average >= 95 && average <= 100)

Oh wait...there's more:
cout << " Your average is: " << average << endl << "You got an A+ ! Congratualtions ! " << endl;

else if (average <= 64) or just else

And remove the do..while statement altogether.
Last edited on
first of all, thank you for the help. I tried what you suggested but it did not work. Here's how I did my if conditions.

if ((average >= 95) && (<= 100))
{
cout << " Your average is: " << average << endl;
cout << "You got an A+ ! Congratualtions ! " << endl;
}
else if ((average >= 90) && (<= 94))
{
cout << " Your average is: " << average << endl;
cout << " You got an A ! Good job ! " << endl;
}
else if ((average >= 85) && (<= 89))
{
cout << " Your average is: " << average << endl;
cout << " You got a B+ ! " << endl;
}
else if ((average >= 75) && (<= 84))
{
cout << " Your average is: " << average << endl;
cout << " You got a C+ ! " << endl;
}
else if ((average >= 69) && (<= 74))
{
cout << " Your average is: " << average << endl;
cout << " You got a C- ! " << endl;
}
else if ((average >= 65) && (<= 68))
{
cout << " Your average is: " << average << endl;
cout << " You got a D ! " << endl;
}
else (average <= 64)
{
cout << " Your average is: " << average << endl;
cout << " You got an F, you fail the class ! " << endl;
}
http://www.cplusplus.com/forum/articles/3483/

In short:
(x<10 && >5) //Wrong!
(x<10 && x>5) //Right!

When writing conditions, don't write them as if you were describing them to a normal person ("if average is at least 95 and at most 100"), write them as if you were talking to someone with brain damage who'd ask you "durr, what is at most on handreed[sic]?", since that's exactly what the computer will tell you.
While writing them, think like this: "if (((average) (is greater than or equal to) (95)) (and) ((average) (is lower than or equal to) (100))". Parentheses not optional.
Last edited on
Ok I see what you're saying but I'm still having issues!!

#include <iostream>

using namespace std;

int main()
{
int firstTest;
int secondTest;
int thirdTest;
int forthTest;
int average;


cout << " Enter the first test score: " << endl;
cin >> firstTest;
cout << " Enter the second test score: " << endl;
cin >> secondTest;
cout << " Enter the third test score: " << endl;
cin >> thirdTest;
cout << " Enter the forth test score: " << endl;
cin >> forthTest;

average = (firstTest + secondTest + thirdTest + forthTest) / 4;


if ((average >= 95) && (average <= 100))
{
cout << " Your average is: " << average << endl;
cout << "You got an A+ ! Congratualtions ! " << endl;
}
else if ((average >= 90) && (average <= 94))
{
cout << " Your average is: " << average << endl;
cout << " You got an A ! Good job ! " << endl;
}
else if ((average >= 85) && (average <= 89))
{
cout << " Your average is: " << average << endl;
cout << " You got a B+ ! " << endl;
}
else if ((average >= 75) && (average <= 84))
{
cout << " Your average is: " << average << endl;
cout << " You got a C+ ! " << endl;
}
else if ((average >= 69) && (average <= 74))
{
cout << " Your average is: " << average << endl;
cout << " You got a C- ! " << endl;
}
else if ((average >= 65) && (average <= 68))
{
cout << " Your average is: " << average << endl;
cout << " You got a D ! " << endl;
}
else (average <= 64)
{
cout << " Your average is: " << average << endl;
cout << " You got an F, you fail the class ! " << endl;
}

cin.ignore();
cin.get();
I'm still having issues

You'll have to be more specific. My psychic clone is not with me, right now.

By the way, I just noticed that every condition in that if has one redundant operand.
1. average can't (well, shouldn't) be >100.
2. If the first condition failed, average can't be >=95, so asking if it's <=94 is redundant.
3. If the second condition failed, average can't be >=90, so asking if it's <=89 is redundant.
And so on. The last condition is completely redundant and should just be an else.
Topic archived. No new replies allowed.