Thread: user can hit enter all day long

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    user can hit enter all day long

    This program works fine for the most part. If you enter 1 or 2 it says what it should, and if you enter another number it works fine as well. However, even though I implemented the method I learned on here a couple days back to clear the buffer, for some reason if you don't type a number and just hit enter, it doesn't go to default like it should. Instead, you can hit enter all day long. What's up with that?

    Code:
    #include <stdio.h>
    int main() {
         
         int cb;
         int a;
         printf("Enter an integer:");
         scanf("%d", &a);
         while ((cb = getchar()) != '\n');
         switch (a) {
    
                case 1:
                printf("\nYou entered 1");
                break;
    
                case 2:
                printf("\nYou entered 2");
                break;
    
                default:
                printf("\nYou made an invalid selection!\n");
                getchar();
                main();
    
                }
    
        getchar();
        return 0;
        }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Also, another small question: Down below where it says "if (today ......" why are there two sets of parenthesis? Wouldn't one set suffice?

    Code:
    #include <stdio.h>
    enum days {monday=1,tuesday,wednesday,thursday,friday,saturday,sunday};
    
    int main()
    {
        enum days today = monday;
    
        if ((today == saturday) || (today == sunday)) <---right here
        {
             printf("Weekend\n");
        }
        else 
        {
             printf("Go to work or school\n");
        }
    
        return 0;
    
    }

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Also having to do with my first question, when I was first trying it, I accidently typed:
    Code:
    while (cb = getchar() != '\n');
    instead of
    Code:
    while ((cb = getchar()) != '\n');
    and it still seemed to work fine. Is it really necessary to have 2 sets of parenthesis, or will 1 work?

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You don't want to call main from your case statement, use a loop instead. Also, scanf will leave data on your stream if it finds something invalid. You should check the return value of scanf.


    See this FAQ for a number of solutions.

    The reason that the user can 'hit enter all day long' is because scanf is skipping whitespace until it finds something to attempt to read as an integer.

    As for your parentheses questions, it's to do with order of precedence. It is okay to do foo == bar || baz == quux without the parentheses because == has higher precedence than ||, but it is not okay to do a = getchar() != '\n' without parentheses because != has a higher precedence than =
    Last edited by cwr; 01-01-2006 at 04:08 AM.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Ok, I think I see what you are saying now, thanks

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Oh, and could you please clarify what you meant by, "use a loop instead"...I'm not sure what you mean, I mean, I know what a loop is, but how to implement one to fix my problem..

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Well, you're calling main() from without your program in the case of an invalid selection. Instead, you should have a loop that exits once a valid selection has been made, otherwise loops around and prompts the user again, if that's what you're trying to achieve.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Oh ok, I see, thanks, and that is what I was trying to achieve

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  3. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM