Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this