Goto statement help

<
}
cout << "\n Enter 1 to rerun and 2 to exit \n";
cin >> topstatementnumber;

while (1 == topstatementnumber){}
goto topstatement;
if (2 == topstatementnumber)
goto topstatement;
}
return 0;
}
>

I am trying to use a goto statement at the end of my code and return back to the beginning.
Don't. While goto is useful in some very specific circumstances, it is in most cases not a good idea. It's a bad habit to get into using it frivolously.

If you want your code to repeat, use a loop.
Last edited on
you appear to have an empty infinite loop.

goto aside,

while (1 == topstatementnumber){} //this is the entire loop. it does nothing forever if its 1. (actually, does this even compile?? I thought you had to have at least a ; in there??)

top:

if(topnum == 1 || topnum ==2) //combine them
goto top;

Ill go a step further ... you may want a function, not just a mega global loop. If you have a large chunk of code to repeat, functions are a great way to do it.


Last edited on
Thank you for the advise, I fixed the problem. It is solved.
to simplify your life you could have used the switch() case function. I do that when it comes to the user deciding options.
Topic archived. No new replies allowed.