Thread: I've a problem. Obviously, else I wouldn't be here.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    I've a problem. Obviously, else I wouldn't be here.

    I have this function, which is meant to get a user's input, validate, and translate it to an integer:
    Code:
    int menu(){
            int rv = 0;
            char in;
            do{
    
                    //Display menu
                    printf("----------------------------\n");
                    printf("|What would you like to do?|\n");
                    printf("|1. View [A]ll Students    |\n");
                    printf("|2. View [R]ecord Number   |\n");
                    printf("|3. View [S]tudent Number  |\n");
                    printf("|4. [Q]uit                 |\n");
                    printf("----------------------------\n\n");
                    printf("Enter your selection: ");
    
    //              while ((in = getchar()) != EOF);
    
                    scanf("%c", &in);
    
                    //Validate
                    if (in == '1' || in == 'A' || in == 'a') rv = 1;
                    else if (in == '2' || in == 'R' || in == 'r') rv = 2;
                    else if (in == '3' || in == 'S' || in == 's') rv = 3;
                    else if (in == '4' || in == 'Q' || in == 'q') rv = 4;
                    else printf ("Invalid entry, please try again\n");
    
            } while (!rv);
    
            return (rv);
    }

    The first time it's called, it works as expected:

    Code:
    ----------------------------
    |What would you like to do?|
    |1. View [A]ll Students    |
    |2. View [R]ecord Number   |
    |3. View [S]tudent Number  |
    |4. [Q]uit                 |
    ----------------------------
    But any additional times I call it, it acts as if I already typed something before going on to what I want:

    Code:
    Enter your selection: a
    ----------------------------
    |What would you like to do?|
    |1. View [A]ll Students    |
    |2. View [R]ecord Number   |
    |3. View [S]tudent Number  |
    |4. [Q]uit                 |
    ----------------------------
    
    Enter your selection: Invalid entry, please try again
    
    ----------------------------
    |What would you like to do?|
    |1. View [A]ll Students    |
    |2. View [R]ecord Number   |
    |3. View [S]tudent Number  |
    |4. [Q]uit                 |
    ----------------------------
    
    Enter your selection:
    As you can see dummied out in the code, I tried clearing the keybuffer, but it didn't help. Anyone know what's going on?

  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
    > while ((in = getchar()) != EOF);
    Put this after your scanf call.
    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
    Nov 2010
    Posts
    14
    That's what I tried before; it ends up making it worse. Specifically, with the extra line of code, it never accepts my input at all.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Easy way to do it:
    Code:
    scanf(" %c", &in);

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Thanks ever so, tabstop.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How many other calls to scanf do you have in your program?
    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.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Only two, both looking for integers, though I do have fscanfs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM