C++ ignoring conditions in an IF statement

Started by
4 comments, last by yaustar 16 years, 10 months ago
i'm working on a 2D scrolling shooter at the moment, and right now i'm adding the shooter part (scrolling to come later :P) my priblem is, i'm using an if statement like so: if (PlayerFiring = 1) { Firing.DrawFlash(0, 120, 0 + Firing.FramePos, 160 + Firing.FramePos, 400, 500, 0); } else { } Trouble is, despite putting a break point at the top of the if statement to ensure the value of "PlayerFiring" is equal to 0, when i let the code run, it always draws the sprite. as you can immagine, this is a little frustrating. anyone know why my program is seemingly ignoring the conditions for this if statement and always running the code within? thanks, Mal'
Advertisement
if (PlayerFiring = 1)
Should be:
if (PlayerFiring == 1)

= is the assignment operator whereas == is the comparison operator. What compiler are you using? It should warn against this (At the very least "Conditional expression is constant").

EDIT: Assuming Visual Studio 2005, go to Project Settings (Alt+F7) -> Configutation Properties -> C/C++ -> General, and set "Warning Level" to "Level 4". You really should be doing this for all projects, since it makes the compiler look out for more things like this.
make your = a == and it should work
if you had written it 1 = PlayerFiring
you would get an error
I think you mean:
if (PlayerFiring == 1)
{
...
}

Edit: too slow, damn login time.
Quote:Original post by Evil Steve
if (PlayerFiring = 1)
Should be:
if (PlayerFiring == 1)

= is the assignment operator whereas == is the comparison operator. What compiler are you using? It should warn against this (At the very least "Conditional expression is constant").

EDIT: Assuming Visual Studio 2005, go to Project Settings (Alt+F7) -> Configutation Properties -> C/C++ -> General, and set "Warning Level" to "Level 4". You really should be doing this for all projects, since it makes the compiler look out for more things like this.



D'oh! i always forget that. thanks
Quote:Original post by Malazar
D'oh! i always forget that. thanks

Here is a tip then, when you do "equal to checks", put the constant on the left side and the variable on the right. ie:
if( 1 == PlayerFiring )

This means when you accidentally do:
if( 1 = PlayerFiring )

This will give a compiler error when you compile.

Steven Yau
[Blog] [Portfolio]

This topic is closed to new replies.

Advertisement