1
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Just slap a do while (true) around the whole thing and replace your gotos with breaks. \$\endgroup\$ – 500 - Internal Server Error Oct 2 '13 at 0:10
  • 3
    \$\begingroup\$ It seems to be becoming more fashionable these days to justify using goto, 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:17
  • 2
    \$\begingroup\$ I knew I'd be entertained by a goto question. What I find amusing is that goto generates so much ire in some people, and yet those same people often embrace exceptions, which are often little more than gotos with hidden destinations (not referring to anyone here). The goto has its place, which is usually bailing out of nested logic on error conditions (as in goto 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
  • 1
    \$\begingroup\$ No argument, but no one's trying to justify the practice of unstructured gotos. \$\endgroup\$ – Carey Gregory Oct 2 '13 at 3:44
  • 1
    \$\begingroup\$ @CareyGregory: You're welcome to your opinion, of course. If you think they "improve the clarity and maintainability of code", then it's your code, your rules. There's no "irrational religious holy war", though. It's just a bad programming practice, not a crime. \$\endgroup\$ – Crowman Oct 3 '13 at 2:46
5
\$\begingroup\$

I'm unsure if the posted code is exactly what's intended functionally, but the following would do the equivalent:

do
{
    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);

    if ( selection < 1 || selection > 3 )
        break;

    switch (selection)
    {
    case 1:
        //do and print stuff here
        break;

    case 2:
        //do and print stuff here
        break;

    case 3:
        //do and print stuff here
        break;
    }
}
while ( GetAsyncKeyState(VK_TAB) );

I really can't remember the last time I used a goto. I think it was in a FORTRAN class.

\$\endgroup\$
  • \$\begingroup\$ @CareyGregory haha I wouldn't say that there's not some good uses for them. I'm sure you had a very good reason. And, OK, maybe I have, too, since the FORTRAN class. But I truly don't remember when it was exactly. :) \$\endgroup\$ – mbratch Oct 2 '13 at 1:34
  • \$\begingroup\$ Thanks! My fault for being lazy when I wrote the example. Mostly because I was reading and writing from and to multi-level pointers. It wasn't representative of my actual code. Which is why I didn't get the exact answers I was looking for. I just edited it to what it should have been originally. However, what you wrote did really help me. I am now using an infinite do while loop and got rid of the goto 's. And thanks for the tip on the switch statement. \$\endgroup\$ – chickeninabiskit Oct 2 '13 at 4:03
1
\$\begingroup\$

Yes. It is acceptable.

You officially have permission to write code any way you want at any time. There are generally several ways to do just about anything. Your way works, and now you have valuable experience building control structures with gotos. That experience will be valuable in the future because other programs use gotos.

That being said, mbratch has put together another way to do it. I prefer his method, as it makes the code easier to read and maintain.

You should study and understand both techniques . Each technique is like an arrow. You put them in your quiver, and can head off to hunt bigger game. The more you know, the more dangerous you will be.

\$\endgroup\$
  • \$\begingroup\$ Anyone can write whatever code they wish, but the point of Code Review is to help you write good code. Ever since Dijkstra's Go to statement considered harmful essay (ACM and widely reproduced elsewhere), programmers have accepted that gotos should be eliminated except where it is impractical to do so. Clearly this code easily can be restructured to eliminate the gotos, so most programmers would agree that a goto-free solution is superior. \$\endgroup\$ – 200_success Oct 18 '13 at 20:05
  • 1
    \$\begingroup\$ I would encourage you to read "go to statement considered harmful considered harmful" \$\endgroup\$ – EvilTeach Dec 14 '13 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy