Below is a simple program that prompts user to enter number. The user is assigned a fail, pass, merit or distinction grade based on the value entered:
#include <iostream>
using namespace std;
// 0-30 Fail
// 31-40 Pass
// 41-50 Merit
// 51 and over Distinction
int main()
{
int yourScore;
cout << "Please enter your score: " << endl;
cin >> yourScore;
if (yourScore <=30)
{
cout << "Your grade is fail. Better luck next time." << endl;
}
else if (yourScore >30 && <=40)
{
cout << "Your grade is pass. Good." << endl;
}
else if (yourScore >41 && <=50)
{
cout << "Your grade is merit. Well done." << endl;
}
else
{
cout << "Your grade is distinction. Excellent." << endl;
}
return 0;
}
Attempting to compile the above code produces following errors:
main.cpp|21|error: expected primary-expression before '<=' token
main.cpp|25|error: expected primary-expression before '<=' token
I’ve tried adding parentheses around >30 && <=40 and >41 && <=50, which produced more errors.
Where am I going wrong?
should be
imagine that each condition accounts for a true or false value. in your code (<=40) is not an actual boolean expression. you have to have to operands