Logical error

pliz help,I want this program to show a message indicating whether a specific price is equal to 111 or not. This program compiles correctly but gives wrong results. I am not sure where the problem is.


if (price ! = 111)
cout << "Price = 111";
else
cout << "Price is not 111";
You are using 'Not Equals' (price !=111) in the if statement, so your code means

if price is not equal to 111
output "Price = 111"
else
output "Price is not 111"

(EDIT : So if you just drop the '!' it will work... ooops)

So if you replace the '!' by '=' it will work.

(Using '=' when you mean '==', get you every time:-)
Last edited on
No, you will actually want == not just =, otherwise C++ will set price to 111 and check if it is true, which it will always be.
 
  if( price == 111 )


or you could obviously say \
if (price != 111)
cout << "Price is not equal to 111";
else
cout << "Price = 111";
Topic archived. No new replies allowed.