-
Goto
I need to know how to use the goto command, I have a list of 'ifs'
something like this:
int main()
{
cout << blah blah blah...if blah blah blah press 1, if blah blah
blah press 2.
if (x==1)
cout << blah blah blah...if blah blah press 3, if blah blah press 4
if (x==2)
cout << blah blah blah...
if (x==3)
cout << blah blah blah...
if (x==4)
cout << blah blah blah...
how then if I needed to go back to if (x==2) from somewhere later on would I get back to it. I have been told to use the goto thing, I was wondering if I can get some more opinions on this.
Thanks.
-
mylabel:
//Other stuff
goto mylabel;
-
This is a old example of creating spaghetti instead of well structured software. You should not use goto. It seems you are creating a large if-construction. I would propose to use the switch-statement and functions.
Whenever it happens that (x == 2), you just call the function that belongs to it.
The switch-statement:
Code:
switch (x)
{
case 1:
/* do something */
break;
case 1:
/* do something */
break;
etc.
...
default:
break;
}