Substitute for goto

closed account (21vXjE8b)
Well, I'm trying to eliminate all the goto in my code, but I can't figure out a way of doing that if the command is inside a do/while loop. Any tips, guys? =)
**Ignore the system(). I'll add a substitute to call for it later... BUT... Feel free to comment how I can do that... =D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
do {
	label_1:
	std::cout << "What do you wanna do?" << std::endl
	<< "1 - Go pick the equipment." << std::endl
	<< "2 - Take a look at the basement." << std::endl
	<< "3 - Do nothing." << std::endl;
	std::cin >> choice;
	
	if (choice == 1) {
		std::cout << "JHENNY: We are ready to go!" << std::endl;
	}
	
	else if (choice == 2) {
		std::cout << "JHENNY: Where are you going?!" << std::endl;
		
		system ("pause");
			
		goto label_1;
	}
	
	else if (choice == 3) {
		std::cout << "JHENNY: What are you waiting for?!" << std::endl;
		
		system ("pause");
		
		goto label_1;
	}	
} while (choice > 3);
Last edited on
Specify the code inside code tags. so that it will be easy to understand

and i for i understand that you can use while inside switch statements
so that break the condition and starts again.

something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(1)
{
print statements
scan

switch()
{
  case:
    break;
 .
 .
 }
  default:
     break;
    // some flag setting to get out from loop ;
}



Last edited on
closed account (21vXjE8b)
Sorry... I was trying to do that, but I was getting an error (permission denied... O.o)
Who tells you "permission denied"?
Can't be the compiler !?

In your case, the easiest thing is to set choice to something above 3 instead of the gotos, so that the while() condition is true after you exit the if() s.
So, you will stay in the loop.

It's a bit dodgy, but it should work.
closed account (21vXjE8b)
Thank you new1! Doing by your way, it gives the expected result, but I have to alter the structure of all the choices...
Reading your comment, plexus, I noticed that if I only alter while (choice > 1) it also gives the expected result... =P
And it was the forum that showed me the message "permission denied"! Not the compiler!
Last edited on
Topic archived. No new replies allowed.