simulate the goto command in basic

I have a program that is supposed to allow the player and the computer to take turns removing straws. Whoever takes the last straw loses.
My problem is that I can't get the program to go back to the lines that let the computer take its turn.
I have tried nesting, but that did not work either. I don't have the entire program here either.


-----------------------------------------
int starting_straws;
int straws_taken;
starting_straws=30;
int straws

//area dictating computer response..for the sake of saving space assume this goes all the way to 30
do
if (straws==3) //I am assuming the computer will win
{
straws_taken=1;
}

else if (straws==4)
{
straws_taken=2;

}
else if (straws==5)
{
straws_taken=1;
}

while(straws>0);


cout << "\n\nHow many straws do you wish to take?\n";
do {
cin >> straws_taken;
if (straws_taken>3)
cout << "\nChoose a number that is 3 or less! Try again!" << endl;
}
while (straws_taken>3);


It is at this point that I need to find a way to make the computer take its turn. Nothing I tried has worked.

PS:I do have code that subtracts the variables so that the number of straws remaining stays current. It works. I just can't figure out how to get back into the loop allowing the computer to take its turn.
What sorts of looping have you covered so far?

Generally, if you have a bunch of logic that does stuff, if you put

1
2
3
4
while (variable > 0) {
   <a bunch of logic doing stuff>
  variable = variable - something;
}

around it, then it will loop while "variable" remains positive. Note that it's very important to change the value of variable somewhere inside your loop (otherwise you'll end up with an "infinite loop" that will never end).

There are several types of loops you can use, depending on whether you want to test the condition initially and possibly not execute the loop at all (the "while( ) { ... }" form above), or always go through the logic at least once and possibly not branch back (the "do { ... } while ( );" form that you've got in your sample code above), or even the ubiquitous for-loop (which is a bit of a shortcut for writing an initialiser, test condition, and an incrementer all in one ("for( ... ; ... ; ... ) { }"). That last one is similar to BASIC's "FOR i = 1 TO 10 ...... NEXT i" construct, just a little more powerful, and the first one is similar to BASIC's "WHILE (condition) ... WEND" (I'm guessing you come from a BASIC background, judging by the title of the question).

HTH (^_^)n
Last edited on
Other than that, there is a goto command in C++, but honestly I can't really think of a situation where you'd ever need it. Or even just where using it would be practical.
I have tried everything it seems except the "for" loop.
Getting the computer to return to the loop(the one that dictates the computer's choices) within the loop seems to be the issue.
In this particular case, a "for" loop wouldn't gain you much more than a "while" loop would (so if a while loop isn't working, then a for loop will have the same problem).

Using goto is frowned upon, majorly (which is why I didn't mention it as a possibility).

Perhaps insert some extra cout's showing the state of variables, if you're not sure why your program is hanging itself.

(( as a side-question, does your computer response follow the pattern "if the number of straws left is odd, then take one straw, otherwise if the number of straws left is even, then take two straws"? if so, then you don't really need to list all thirty alternatives - that's a lot of typing X( - perhaps read up about the "%" modulus operator ;-) )).
Last edited on
Topic archived. No new replies allowed.