What Is Wrong With My If Statement?

I am writing a GPA program for my C++ class. What I am trying to do what is above, and beyond what my professor is requiring us to do. I have is so the user enters their letter grade when prompted. I want to make it so when a letter other than an A, B, C, D, or F is enter the program terminates, and tells them they entered an invalid letter grade. This is what I did for my if statement.

grade1 is a char variable where I store the inputed character grade.

if ( grade1 != 'A' || 'B' || 'C' || 'D' || 'F')
{
cout << "You have enter an invalid letter grade." << endl;
return 0;
}

When running the program the if statement is true for any letter than I enter. What did I do wrong?

1
2
3
4
if( ( grade1 != 'A' ) || ( grade1 != 'B' ) )
{
    //code...
}


You need to compair each check. (:
Last edited on
Oh!!! Thank you!
No problem. (:
Topic archived. No new replies allowed.