Thread: menu help

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    10

    menu help

    I've had a search of the forums and i foudn some code for a menu. I've basically got my game up and running and thought a menu system might be good.

    this code works apart from it exit's what ever you type.

    I'm also quite stuck on how to get the first option to just continue with the game. I've tried adding initalise to it but that does not seem to do it

    Any one got any ideas?
    Code:
    {
      int selection;
      printf("1. Start Game\n2. Exit\n");
      printf("\nPlease Choose an option: \n");
      scanf("%d", &selection);
      
      switch(selection){
        case 1:
          intialise();
          break;
        case 2:
          exit(1);
          
        default:
          printf("Invalid selection\n");
          break;
      }
      return 0;
    }
        
        intialise();
    
    /* Rest of my code is here*/

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's too incomplete to comment
    For starters, the menu code should be in some kind of loop, otherwise you just get to ask the question once, and then the program exits anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    10
    Thats all i need. I only need it to ask it at the begging of the program.

    Welcome

    Press S or 1 to start
    Press E or 2 to exit.

    Thats it.

    It's just a simple if user selects 1 then continue if user selects 2 exit menu.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    hope this helps..

    Code:
    /*function prototypes */
    void menu(void);
    void game(void);
    
    int main(void)
    {
        menu();  
    
        return 0;
    }
    
    void menu(void) {
        int selection;
    
        printf("1. Start Game\n");
        printf("2. Exit            \n");
        printf("\nPlease Choose an option: \n");
        scanf("%d", &selection);
      
        switch (selection) {
            case 1:  game();
                     break;
            case 2:  exit(0);           /* exit game */    
          
            default: printf("Invalid selection\n");
                     menu();          /* simple recursion, can be accomplished with while(); */
         }
    }
    
    void game(void)
    {
        /* insert game-code here */
    }
    Last edited by zuiplap; 05-05-2004 at 09:46 AM.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    10
    Cheers mate thanks for the help. and thanks for your help in other threads as well!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM