Jump to content
  • Advertisement
Sign in to follow this  
Malazar

C++ ignoring conditions in an IF statement

This topic is 4075 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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!