Help with if/else statements

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main()
{
string name; //the name to be inserted
int test1, test2, test3, test4, test5; // the test score values
double result; // variable use to hold the average of the test scores
char answerA = 'Y', answerB = 'N';


cout << "This program will calculate the average of your five test scores.\n";
cout << "So I can better help you what is your name? \n";
cin >> name;
cout << "\n";
cout << "Nice to meet you " << name;
cout << " Please enter your five test scores so the average can be calculated ";
cout << " Please enter score 1:" ;
cin >> test1;
cout << " Please enter score 2: ";
cin >> test2;
cout << " Please enter score 3: ";
cin >> test3;
cout << " Please enter score 4: ";
cin >> test4;
cout << " Please enter score 5: ";
cin >> test5;

result = (test1 + test2 + test3 + test4 + test5) / 5;
cout << "Your average is a " << setprecision(2) << showpoint<< result;
cout << "\n";

if (result >= 95) // if average of test scores is greater then or equal to 95 message 1 will print out if not message 2 will print
{
cout << "Congratulations you have a high gpa";

if (result == 100)
{
cout << " Congrats you made the highest score possible ";
}

}
/* else
{
cout << "You did not score high enough keep trying\n";
cout << "Run the program ";

}*/


if(result >= 70 && result <= 90)// If score is between 70 or 90%
{
cout << "Your score is passing however, it is no 100%";
cout << " Although you scored a " << result << "% there is always room for improvement"

}




else
{
cout << "You did not score high enough keep trying\n";
cout << "Run the program ";

}

}
Last edited on
C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

if(result >= 70 && <= 90)
Should be:
if(result >= 70 && result <= 90)

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I'm trying not be a peon here but the formatting is doing it's job.
Within the 70-90 condition:

cout << " Although you scored a " << result << "% there is always room for improvement"
is missing a semicolon.

Edit: Also, the conditional statements probably don't cover the full range of averages as desired. Look carefully at the ranges and see if anything is missing (if any scores might "slip through, so to speak).
Last edited on
Thanks I just wanted to test out nested ifs. My next one is to to if if else and cover the full grading scale with user validation.
Topic archived. No new replies allowed.