Thread: One last question for the day

  1. #1
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question One last question for the day

    Greetings,

    I have written a simple menu program, and am trying to get my error checking to work. What I am having problems with is if I enter a letter rather then a number. If I enter a number that is not an option, everything works great, but if I enter a letter, like F of H I get in some loop, and can not get out. I did search the boards and found a few things, but nothing that seems to clear up my issue, unless I want to rewrite my code. Obviously if I am way off, and need to rewrite it I can, but I am hoping someone might have an idea how I can correct my issue without redoing the whole thing.

    Here is my code:

    // Menu Driven Program

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>


    int getMenu (void);
    void function1 (void);
    void fnction2 (void);
    void function3 (void);

    int main (void)
    {
    int done = 0;
    int choose;

    while (!done)
    {
    choose = getMenu ();
    if (choose == 4)
    done = 1;
    else
    switch (choose)
    {
    case 1: functionq ();
    break;
    case 2: functions2 ();
    break;
    case 3: function3();
    break;
    }
    printf("\n\n");
    }
    return 0;
    }


    // --------------------getMenu--------------------

    int getMenu (void)
    {

    int choice;


    do
    {
    printf("\t**************************************** \n");
    printf("\t* Menu *\n");
    printf("\t* *\n");
    printf("\t* 1. Option 1 *\n");
    printf("\t* 2. Option 2 *\n");
    printf("\t* 3. Option 3 *\n");
    printf("\t* 4. Exit Program *\n");
    printf("\t* *\n");
    printf("\t**************************************** \n\n");

    printf("Please select a menu optionh (1-4) and press <retrun>: ");
    scanf("%d", &choice);

    if (choice <1 || choice >4)
    printf("\aSorry, but this is not a valid option. Please select again.\n\n");

    } while (choice <1 || choice >4);

    return choice;

    }

    All ideas and help is appreciated.

    Thanks,
    DD
    "aut vincere aut mori"

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%d", &choice);
    should be something more like
    Code:
    if ( scanf("%d", &choice) != 1 ) {
      /* Inform the user */
      puts ( errMsg );
      /* Remove the culprit */
      while ( getchar() != '\n' );
      /*
      ** Further error handling goes here.
      */
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question Still not sure

    If I replace the:

    if (choice <1 || choice >4)
    printf("\aSorry, but this is not a valid option. Please select again.\n\n");

    with
    if ( scanf("%d", &choice) != 1 ) {
    puts ( errMsg );
    while ( getchar() != '\n' );
    }

    I start to have problems with all my number options. I am not sure why this happens. If I select a valid option now, it does not accept my first entry, and I have to do it twice. (i.e. 2 2 to get option 2 to work. But it does resolve all my issues with loop I was getting when I entered an letter instead of number.

    Any other thoughts?

    Thanks,
    DD
    "aut vincere aut mori"

  4. #4
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Thumbs up Hooray, got it.

    OK, finally got it. The line:

    while ( getchar() != '\n' );
    Was the magical option for me. I kept everything else the same, but added that line in, and it now handles my problem with the letters and numbers...See below:

    scanf("%d", &choice);
    if (choice <1 || choice >4)
    {
    printf("\aSorry, but this is not a valid option. Please select again.\n\n");
    while ( getchar() != '\n' );
    }
    }
    while (choice <1 || choice >4);

    return choice;

    Thanks again for the help. I apprecaite you getting me in the right course

    DD
    "aut vincere aut mori"

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I am not sure what other problems you are getting, but here's an alternate version of you getMenu function.
    Code:
    int getMenu(void)
    {
        int choice, rc;
    
        while (1)
        {
            printf("\t****************************************\n");
            printf("\t* Menu *\n");
            printf("\t* *\n");
            printf("\t* 1. Option 1 *\n");
            printf("\t* 2. Option 2 *\n");
            printf("\t* 3. Option 3 *\n");
            printf("\t* 4. Exit Program *\n");
            printf("\t* *\n");
            printf("\t****************************************\n\n");
    
            printf("Please select an option and press <enter>: ");
            rc = scanf("%d", &choice);
            while (getchar() != '\n');
            if (rc != 1 || choice < 1 || choice > 4)
                printf("\aSorry, but this is not a valid option. Please select again.\n\n");
            else break;        
        }
    
        return(choice);
    }
    See if this helps you or not.

    [EDIT]Oopps, I see I'm slow posting this one, you've fixed it already!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. day of week
    By s_ny33 in forum C Programming
    Replies: 18
    Last Post: 11-02-2005, 11:56 AM
  3. Should I learn a FULL language first?
    By Raeliean in forum Game Programming
    Replies: 8
    Last Post: 07-16-2005, 06:59 PM
  4. stupid question of the day
    By davidnj732 in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 02:46 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM