Thread: Going to without using goto!?!!?

  1. #1
    Registered User
    Join Date
    Dec 2003

    Going to without using goto!?!!?

    Hi people,

    I'm be gentle! I need some help in that I am trying to write a program which uses an initial menu system and then performs tasks as per the task number selected(1,2,3 etc). The problem is that i want the program to be able to jump back to the menu system during some of the tasks at certain points (ie. if an invalid input is entered) to give the user the option to select a task again. Does anyone know of a way to do this without using a 'goto' command. Hope this makes sense.......
    Help would be greatly appreciated!!!!

    Many Thanks,

  2. #2
    Join Date
    Nov 2003
    Put the initial menu system inside it's own function.
    Then you can call it anytime.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Cape Town
    Basically you would use some sort of loop (to go back to the menu). Inside the loop, you would check to see if the input was right. If it was, then call whichever function, otherwise continue the loop.

    A swicth inside a loop works well.

    int bDone = 0;
    while( !bDone )
        printf( "\nWelcome\n");
        printf( "1. New\n" );
        printf( "2. Open\n" );
        printf( "3. Save\n" );
        printf( "4. View\n" );
        printf( "5. Exit\n" );
        printf( "\nEnter an option: " );
        switch ( fgetc( stdin ) )
             case 1: function1(); break;
             case 2: function2(); break;
             case 3: function3(); break;
             case 4: function4(); break;
             case 5: bDone = 1; break;
             default: //The user typed an invalid option

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >Does anyone know of a way to do this without using a 'goto' command.
    #include <stdio.h>
    int main ( void )
      int ch;
      printf ( "1) Item 1\n2) Item 2\n3) Item 3\n" );
      printf ( "Enter a selection: " );
      fflush ( stdout );
      ch = getchar();
      while ( getchar() != '\n' )
      if ( ch < '1' || ch > '3' )
      return 0;
    Or you could use setjmp and longjmp, but most programmers are silly and use structured programming techniques like functions and loops. What a bunch of goofballs, eh?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Thanks everyone, I used the function solution in the end, still trying to understand all the case and break stuff! (first year programming at uni!!!). Got a whole new set of problems now, but going to have a go myself before asking on here.....( lot are gonna grow to love me!!).
    Thanks again for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM