Sign in to follow this  
Timmygyu

Ignored if statement

Recommended Posts

Timmygyu    180
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
Drew_Benton    1861
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   
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
tehMatthewJ    122
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

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