# C++ ignoring conditions in an IF statement

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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'

##### Share on other sites
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.

##### Share on other sites
make your = a == and it should work
if you had written it 1 = PlayerFiring
you would get an error

##### Share on other sites
I think you mean:
if (PlayerFiring == 1)
{
...
}

Edit: too slow, damn login time.

##### Share on other sites
Quote:
 Original post by Evil Steveif (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

##### Share on other sites
Quote:
 Original post by MalazarD'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.

##### Share on other sites

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.