Jump to content
  • Advertisement
Sign in to follow this  
Timmygyu

Ignored if statement

This topic is 4830 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

Hi, I have a problem. The game I'm writing has places where you can take two or three different pathes that reconverge later on. For example: Use the collum on the left or the collum on the right. The problem I'm having is that when I break out of those choice branches I have a nesicary if...else statement. My problem is that my if else statement is being completely ignored. I can't figure out why this is happening and I've spent long hours tring to figure it out. Any help? Here an example for clarification: //////////////////////////////////////////////////////////////////////////////// #include <iostream> int main() { int foobarError = 0; int foobar1 = 0; std::cout << "BlahBlahBlah/n"; std::cin >> foobar1; if(foobar1 = 1) { int foobar2 = 0; std::cout << "foobar at one!/n"; std::cin >> foobar2; if(foobar2 = 2) { std::cout << "foobar at two!/n"; goto foobarTransport; } else ( std::cout << "foobar in error.../n"; foobarError = 1; } } else ( std::cout << "foobar in error.../n"; foobarError = 1; } { foobarTransport: if(foobarError = 0) { std::cout << "foobar is the winner!!!/n"; } else if(foobarError = 1) { std::cout << "foobar in horrid error.../n"; } return 0; } //////////////////////////////////////////////////////////////////////////////// In this case if you type in 5 in the first or second cin statement you would recive the message "foobar in error..." on the screen. Afterwards, to my understanding, the message "foobar in horrid error..." should be displayed. Instead the message "foobar is the winner!!!" is displayed. Why is this and how do i get around it? Bye, Tim

Share this post


Link to post
Share on other sites
Advertisement
You have: if(foobar2 = 2) when that is supposed to be if(foobar2 == 2) That and all your = should be == in all your if statements [wink] Whats happening is that you are assigning the values, which will always evaluate to true as long as the variable is not 0.

You can also use the [source] code goes here [/source] tags to format your code nice and neatly [smile]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And the use of 'goto' is not exactly a recommended practice. You are better off refactoring your code so that you don't need to use this construct.

Share this post


Link to post
Share on other sites
It may not be the problem, but I see that you have two if's and an else. Should that second if be an "else if" for exclusivity? In your case, the else could happen even if the first if happens, because they are not lumped together. I'm not sure if this is the solution to your problem or even a problem at all, but it caught my eye. Cheers.

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!