Thread: Checking input in a while loop...

  1. #16
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Ok, I guess I've had a few too many beers today...sheesh.

    Apologies to both Click_here and c99tutorial (although Click_here you are guilty of reposting as C99 posted the same solution already). For some reason I thought you guys were just telling him how to get rid of whitespace first...

    At any rate, regarding your other points I most certainly understand the issue, as my posts above already answered the question most succinctly for the poster (whos not even reading this thread anymore).

    Again, my deepest apologies I jumped the gun and didn't fully analyze the code you guys submitted. So many "how to consume whitespace" posts I just got overzealous I guess.. :P

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Melbourne Australia
    It's ok, we all have those days - That's why they put an eraser on the back of pencils.

    I try to avoid
    * Posting a fact that I can't find a source for .
    * Posting code that has not been compiled & tested.

    So when I have those days, I can catch myself out before posting it.

    Here is a sample code showing what the problem is.

    After a failed attempt to read, it will show you what was left on stdin

    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
        unsigned long darts;
        while((scanf("%lu", &darts)) != 1)
            int c;
            while ((c = getchar()) != '\n' && c != EOF)
         return EXIT_SUCCESS;
    Last edited by Click_here; 01-31-2013 at 09:38 PM.
    Fact - Beethoven wrote his first symphony in C

  3. #18
    Registered User
    Join Date
    Sep 2011
    Dongargarh, India
    The OP may not have checked this thread. But it was quite informative for me, still a n00b.
    Thanks for this lively discussion.

  4. #19
    Registered User
    Join Date
    Nov 2012
    Quote Originally Posted by nonpuz View Post
    #include <ctype.h>
       while((scanf("%lu", &darts)) != 1)
          printf("\n\nThis is not an acceptable entry. please try again.\n");
          printf("%s", prompt);
          int c;
          while (!isdigit(c = getchar()) && c != EOF)
          if (isdigit(c)) ungetc(c, stdin);
    The problem with this approach is that it will correctly reject non-numeric input like "Hello world", but it will accept invalid input such as
    Hello world123
    In that case I would prefer to treat that input as an error and ask for new input. Using the approach from nonpuz will instead treat the above as a valid integer 123.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop not checking if VE>0
    By JonathanS in forum C Programming
    Replies: 11
    Last Post: 10-02-2011, 10:15 AM
  2. checking input
    By deviousdexter in forum C# Programming
    Replies: 23
    Last Post: 12-19-2008, 05:20 PM
  3. checking input
    By mackol in forum C++ Programming
    Replies: 4
    Last Post: 03-16-2003, 10:47 PM
  4. checking input int
    By hurry up in forum C Programming
    Replies: 4
    Last Post: 03-15-2003, 04:15 AM
  5. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM