# Thread: A simple != problem

1. ## A simple != problem

Code:
``` //This program illustrates the use of logical operators

#include <iostream>
using namespace std;

int main()

{

char year;
float gpa;

cout << "What year student are you ? " << endl;
cout << "Enter 1 (freshman), 2 (Sophomore), 3 (junior), or 4 (senior)"
<< endl << endl;
cin >> year;

cout << "Now enter your GPA " << endl;
cin >> gpa;

if  (gpa >= 2.0 && year == '4')
cout << "It is time to graduate soon" << endl;

else if (year != '4' || gpa <2.0)
cout << "You need more schooling" << endl;

return 0;

}```
I am fairly new to C++, as you can probably tell from the difficulty, or lack of, of this program.

I need to know how to make the line in bold work correctly using the NOT operator, I have exhausted all options I can think of and can't seem to get the program to work correctly.

-Ryan

2. Why do you need to use not? Doesn't it work just fine as is? You could do gpa!<=2.0 or what I just typed w/o the equals.

Edit: You can also do something like !(gpa<=2.0) and !(!year=='4')
There is all kinds of different ways to do it.

3. Well, if you want essentially the same test but using the ! operator somewhere, basically what you'd do is do the 'opposite' comparisons, and then stick it in () and tack a ! in front For example, the condition in the 'else if' is exactly the opposite of the condition in the 'if' block (note that these conditions are copied and pasted from your own code!):
Code:
```(year != '4' || gpa <2.0)
is equivalent to
!(gpa >= 2.0 && year == '4')```
Aside from that, the condition in the 'else if' is unnecessary, since it basically covers all possibilities that the 'if' condition misses. Since this is the case, an 'else' will do the exact same thing as your 'else if' but more efficiently.

4. Ya I should have been more clear, it works fine as is, my professor gave me the extra task of making the program work in the same fashion using the not operator in this line of code.

I can get it to work using the != but when you enter 4 as the grade and enter a GPA under 2.0, it still says "It's time to graduate soon".

5. Well, what exactly is the condition you tried? It's kind of hard to tell what's wrong with it unless you show us what it is Also, if you read between the lines in my earlier post, you'll find pretty much the solution to your question.

*P.S. Welcome to the boards at CProg