Thread: Help with menu and integer selections

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    9

    Help with menu and integer selections

    I have made a program that offers the user several options from which to choose from. Each one has an integer assigned (e.g. 1,2,3...) and the user enters the number of the option he has selected. I use the scanf function to get the user's choice and the switch statement to branch through the program. Still if I am careless enough to enter a letter instead of a number the program will crash. How can I prevent that?

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    you can make this in a lot of ways, using a do-while to check whether it's an valid input, but you can use a tricky-way that not always works but sometimes can be very usefull, look at this:

    Code:
    if (!scanf("%d",&n))
        //invalid input here, error handling
    else
        //valid input, the user did enter an integer
    But this is a problem when scanf() returns EOF
    Also you can do a do-while to check if the input is alphabetic or isdigit, by using the isalpha or isdigit function.

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Here's one way to get bulletproof menus with scanf
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int option;
        
        while (1)
        {
            int rc;
            
            printf("1. Option 1\n2. Option 2\n3. Option 3\n");
            printf("Select an option (ctrl+z to quit): ");
            fflush(stdout);
            
            rc = scanf("%1d", &option);
            if (rc == EOF)
                break;
            
            scanf("%*[^\n]");
            getchar();
            
            if (rc < 1)
            {
                fprintf(stderr, "Bad input, try again...\n");
                continue;
            }
            
            switch (option)
            {
            case 1:
                printf("Option 1\n");
                break;
            case 2:
                printf("Option 2\n");
                break;
            case 3:
                printf("Option 3\n");
                break;
            default:
                fprintf(stderr, "Invalid option\n");
                break;
            }
        }
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. main menu
    By bazzano in forum C Programming
    Replies: 25
    Last Post: 04-25-2007, 06:18 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Dynamic Menu Creation and Processing...
    By cpeschke in forum Windows Programming
    Replies: 1
    Last Post: 03-11-2005, 09:43 PM