Like Tree4Likes

Checking input in a while loop...

This is a discussion on Checking input in a while loop... within the C Programming forums, part of the General Programming Boards category; Ok, I guess I've had a few too many beers today...sheesh. Apologies to both Click_here and c99tutorial (although Click_here you ...

  1. #16
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    504
    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
    Location
    Melbourne Australia
    Posts
    1,500
    [edit]
    nonpuz:
    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.
    [/edit]

    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

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        unsigned long darts;
    
    
        printf(">>");
        fflush(stdout);
    
    
        while((scanf("%lu", &darts)) != 1)
        {
            int c;
    
    
            while ((c = getchar()) != '\n' && c != EOF)
            {
                putchar(c);
                //continue;
            }
    
    
            printf("!\n>>");
            fflush(stdout);
        }
    
    
         return EXIT_SUCCESS;
    }
    Enjoy.
    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
    Location
    Dongargarh, India
    Posts
    16
    Well...
    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
    Posts
    1,218
    Quote Originally Posted by nonpuz View Post
    Code:
    #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
    Code:
    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.

Page 2 of 2 FirstFirst 12
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, 11: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, 04:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21