Why does this keep happening?

This is a discussion on Why does this keep happening? within the C++ Programming forums, part of the General Programming Boards category; OK so I made a program where the user can pick what to do. I created a simple 'if' statement ...

A misstake you will continue to make over time, and it will frustrate you just as much every time because later you wont see it even though you know about it.

a = 1 makes the value of a, 1
a == 1 , checks to see if a is equal to 1

Indeed. One little habit that can be helpful, if you intend a comparison with a constant value, to put the constant on the left hand side of the expression;

Code:

if( 1 == a) something();
if ('R' == answer) something_else();

This will trigger a compiler error should you do assignment (one = sign) rather than comparison.

It doesn't stop problems if the code is comparing two variables (as either assignment will often be acceptable to the compiler). But being in the habit of asking yourself what goes on the left hand side will often have a side effect of making you think about what you're typing.....