Thread: user menu

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    32

    user menu

    what is the best way to have a menu and
    ask for input and check for it.
    this is what i have so far, its not finished
    but i feel im not doing it right
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXCUST 12
    
    struct airline {
           unsigned int seat;
           char customer[MAXCUST];
    
    };
    
    char showmenu ( void );
    
    
    int main(int argc, char *argv[])
    {
        struct airline seat[BUFSIZ];
        char choice;
        
    while ( ( choice = showmenu() )!= 'f' );
        {
            switch ( choice )
            {
              case 'a' : printf(" you hit a");
                   break;
              
              case 'b' : printf(" you hit b");
                   break;
              
              }
                   
      system("PAUSE");	
      return 0;
    }
    }
    
    char showmenu ( void )
    {
      char ans;
      printf(" ********** Welcome to Swift Lines Airlines ************\n");
      printf("\n ********** To Choose A Function, Enter its Letter Label:\n");
      printf("\na ) Show Number of seats\n");
      printf("b ) Show List of seats\n");
      printf("c ) Show alphabetical list of seats\n");
      printf("d ) Assign a Customer to a seat assignment\n");
      printf("e ) Delete a seat assignment\n");
      printf("f ) Quit\n");
      
      ans = getchar();
      ans = tolower( ans );
      
      while ( strchr ( "abcdef" , ans ) == NULL )
      {
            puts( " please pick A - F :\n");
            ans = tolower( getchar() );
            }
       return ans;
    }
    output:
    Code:
     ********** Welcome to Swift Lines Airlines ************
    
     ********** To Choose A Function, Enter its Letter Label:
    
    a ) Show Number of seats
    b ) Show List of seats
    c ) Show alphabetical list of seats
    d ) Assign a Customer to a seat assignment
    e ) Delete a seat assignment
    f ) Quit
    a
     ********** Welcome to Swift Lines Airlines ************
    
     ********** To Choose A Function, Enter its Letter Label:
    
    a ) Show Number of seats
    b ) Show List of seats
    c ) Show alphabetical list of seats
    d ) Assign a Customer to a seat assignment
    e ) Delete a seat assignment
    f ) Quit
     please pick A - F :
    why is it doing this?
    thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C a line of code is terminated with the semi-colon. In this line of code, you tell the program to call showmenu(), over and over, until the choice is 'f'.

    Code:
    while ( ( choice = showmenu() )!= 'f' );  /* <--note the semi-colon here */
        {
            switch ( choice )
            {
              case 'a' : printf(" you hit a");
                   break;
              
              case 'b' : printf(" you hit b");
                   break;
              
              }
    If you move the semi-colon, you'll get a completely different result.

    What I do is call the menu() function, and everything loops back to that function:

    Code:
    menu();
    
    Then in menu(), I use a while(1) loop, to keep everything looping back, and include a line like this:
    
    while (1)  {
       /* get user's choice here */
       /*handle other choices, of course (probably in a switch statement), which includes this: */
       if (choice == 'q' || choice == 'Q")
          break;
    };  /*note the semi-colon placement */
    Hope that helps.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    ahhhhh thanks i didnt even notice that!
    it works now...
    ok i will check out your way and see what works ok for me..
    thx

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    why is it doing this?
    doing what?
    Code:
              }
      }
      system("PAUSE");	
      return 0;
    }
    edit: beat

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Quote Originally Posted by robwhit View Post
    doing what?
    Code:
              }
      }
      system("PAUSE");	
      return 0;
    }
    edit: beat
    what it was doing was
    displaying the menu twice and even though i selected a choice it was reading i wasnt.. but its fixed due to the missplaced ;

    as for the system pause it was placed there by devc++ and i havent removed it!
    thx

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    It was inside the loop. I assume you wanted it to be the last thing to be run, and not make the loop break out prematurely. If I was wrong, then, you are right. Otherwise, see what happens if you enter a wrong value.

    I also made my first post when I thought that there were no replys yet. Sorry if that was confusing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  4. Systray menu messages
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-09-2005, 06:25 AM
  5. C menu driven program
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 08:56 AM