So, I made a console interface. It prompts users with several options they can enter. 1 for the first option, 2 for the second option, etc. Using the switch statement, each option will bring them to a different screen. Now, after any of the options are entered and the users are brought to a particular screen, how do I get them back to the selection screen with those options? Would it be acceptable for me to do something along the lines of
selectionScreen:
printf("Enter 1 for the First Screen\n");
printf("Enter 2 for the Second Screen\n");
printf("Enter 3 for the Third Screen\n\n");
scanf("%d", &selection);
switch (selection)
{
case 1:
//do and print stuff here
for (i = 0 ; ; i ++)
{
//do and print stuff here
while(GetAsyncKeyState(VK_TAB))
{
goto selectionScreen;
}
printf("Enter the Tab key to go back the selection screen.\n\n");
printf("Enter any key besides Tab to reload!\n\n");
getchar();
system("CLS");
}
break;
case 2:
//do and print stuff here
for (i = 0; ; i++)
{
//do and print stuff here
while(GetAsyncKeyState(VK_TAB))
{
goto selectionScreen;
}
printf("Enter the Tab key to go back the selection screen.\n\n");
printf("Enter any key besides Tab to reload!\n\n");
getchar();
system("CLS");
}
break;
case 3:
//do and print stuff here
for (i = 0 ; ; i++)
{
//do and print stuff here
while(GetAsyncKeyState(VK_TAB))
{
goto selectionScreen;
}
printf("Enter the Tab key to go back the selection screen.\n\n");
printf("Enter any key besides Tab to reload!\n\n");
getchar();
system("CLS");
}
break;
default:
//do stuff here
}
Or is there a more favorable way of doing this
do while (true)
around the whole thing and replace yourgoto
s withbreak
s. \$\endgroup\$ – 500 - Internal Server Error Oct 2 '13 at 0:10goto
, and to recommend its use to beginners. IMO it's better to avoid it entirely, and on those rare occasions where it might actually be justified, just make your code very slightly more verbose than it needs to be. If you try to justify it, then I think you'll end up just justifying it too much and thinking every time is an exception, and I think it's best not to go down that road. \$\endgroup\$ – Crowman Oct 2 '13 at 0:17goto
question. What I find amusing is thatgoto
generates so much ire in some people, and yet those same people often embrace exceptions, which are often little more thangoto
s with hidden destinations (not referring to anyone here). Thegoto
has its place, which is usually bailing out of nested logic on error conditions (as ingoto Failure;
). But as a control flow mechanism? Bad idea in most cases. But bailing out to a single error handling point is often a perfect solution that simplifies and enhances maintainability. \$\endgroup\$ – Carey Gregory Oct 2 '13 at 1:23