This is going to be a long post, so bear with me…
I decided to learn C++ and I’m doing great, until I got to the NOT statement.
First, let me say, I understand && and || statements perfectly. I wrote my own programs, compiled, tested and they worked great.
Second, I understand the NOT statement but I don’t know how to apply it to a test program. I understand that if the code evaluated returns false, it satisfies the NOT statement making it TRUE. ie. ! (6 <= 4) - it would return TRUE because 6 IS NOT less than or equal to 4.
So with that said, I wrote this expecting to not satisfy the NOT statement making it true, but the output was not what I wanted to.
Also, when I compile, Terminal did not find any BUGS, so it’s not my programming
Here is is:
// NOT Statement - demonstrate the NOT statement
#include
#include
#include
using namespace std;
int main(int nNumberofArgs, char * pszArgs[])
{
// declare a variable to store an entered number to be tested with the NOT statement
int nNum;
cout << "Enter a number that is not equal to or less than 4: ";
cin >> nNum;
// by declaring a number that is not equal to or less than 4 we will satisfy the NOT statement
// now create the IF statement to test the NOT statement
if ( ! nNum <= 4)
{
cout << "The number you entered was not less than or equal to 4" << endl;
}
else
{
cout << "The number you entered was less than or equal to 4" << endl;
}
// Ergo if we enter say '6' we will get the first output because the argument on the right is true making the expression false
// If I would have put say '3' that would make the expression false because the argument on the right is true: 3 is less than or equal to 4
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
So I compiled and ran the program. I entered ‘6’ and I got the first output - like expected.
I then re-ran the program this time entered a number that would make the expression false because the argument on the right would be true, entered 3, but I got the first output “The number you entered was not less than or equal to 4”, but 3 is less than 4, so my program failed to execute what I wanted it to.
Can anyone help me? To me this is the error, but i don’t know what the right code is:
if ( ! nNum <= 4)
I believe that my syntax or the NOT expression is incorrect…