code help

Why doesn't this work?

#include <iostream>
using namespace std;
int main()

{
int x;
cout << "what percent did you get? \n";
cin >> x;

if (x <= 100 && x >= 90);
cout << "perfect A";
else if (x <= 90 && x >= 80);
cout << "B";
else if (x <= 80 && x >= 70);
cout << "C";
else if (x <= 70 && x >= 60);
cout << "D";
else if (x<60);
cout << "F";

else
cout << "no valid data entered";

}
Last edited on
Your logic is faulty. It should be 100-90 == A, 89-80 == B, 79-70 == C, 69-60 == D, 59-0 == F

You are doing 100-90 == A, 90-80 == B, 80-70 == C, 70-60 == D, 59-0 == F

Also, be a little more specific than "why doesn't it work" you're supposed to tell us what your errors are so we can help you to fix them not spot the errors for you.
'else' without a previous 'if' for every statement
You have an extra semi-colon after each if statement. So remove those and fix your logic and it should work as expected.
Thank you, i got it working :D

However, now it always gives me the else statement. For example, if i put in a number, it will give me the correct Grade, but it will throw in a "no valid data" after it regardless. Is there a way to fix this?

Do i have to add brackets to each if statement, and then make more codes and all that?

#include <iostream>
using namespace std;
int main()

{
int x;
cout << "what percent did you get? \n";
cin >> x;


if (x <= 100 && x >= 90)
cout << "perfect A";
else if (x <= 89 && x >= 80)
cout << "B";
else if (x <= 79 && x >= 70)
cout << "C";
else if (x <= 69 && x >= 60)
cout << "D";
else if (x<59)
cout << "F";

else(x<0 && x>100);
cout << "no valid data entered";

}
Last edited on
else if (x<0 && || x>100); <--- remove that semicolon, add if and make logical OR, not and. (The number can't be both less than 0 and more than 100)
Last edited on
Thank you!
Last edited on
Topic archived. No new replies allowed.