Going to without using goto!?!!?

This is a discussion on Going to without using goto!?!!? within the C Programming forums, part of the General Programming Boards category; Hi people, I'm new...so be gentle! I need some help in that I am trying to write a program which ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    Going to without using goto!?!!?

    Hi people,

    I'm new...so 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,
    CoDe.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    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
    Location
    Cape Town
    Posts
    788
    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.

    Code:
    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
    Posts
    9,796
    >Does anyone know of a way to do this without using a 'goto' command.
    Sure!
    Code:
    #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' )
        main();
    
      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
    Posts
    2
    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.....(christ...you lot are gonna grow to love me!!).
    Thanks again for the help.
    CoDe.

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, 11:32 AM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21