Thread: Validation

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Validation

    Code:
        
    do           
     {
                    // prompt and read salesman id
                    printf("Salesman ID (NO.): ");
                    scanf("%d",&stop);
                    if (stop == NULL)
                    {
                        valid = 0;
                        printf("Invalid input. Please input again.\n\n");
                    }
                    else
                        valid = 1;
                } while (valid == 0);
    How can I fix it to avoid the continuous looping?

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    scanf returns the number of items that it successfully converted. Check that it returns 1. If it doesn't return 1 you'll need to clear the input buffer before you try to call scanf again (and NOT with fflush(stdin)).

    Edit: unless, of course, it returns EOF, then you will probably want to exit or something because that's a read error or end-of-file condition.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    Does it matter if behaviour is undefined?

    FAQ > Flush the input buffer - Cprogramming.com
    I can't understand how it works.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Does it matter if behaviour is undefined?
    Um, yes. A program is supposed to behave in a predictable manner. If it doesn't, then it is useless, if not downright dangerous.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    So, is it means keeping moving the input buffer to ch and reset it just like no input before? Is \n also loved to ch? What is the purpose of EOF? The program still works fine without EOF.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Anybody can help me?

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by ulti-killer View Post
    Anybody can help me?
    From MSDN site:
    "To indicate a read error or end-of-file condition, getc and getchar return EOF"

    And the reason that you keep getting things off the stdin until '\n' is found is because it is most likely the last thing that was entered.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Is \n also moves to ch too?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Keyboard input is generally line buffered. This means that when you type something, it's stored in a buffer and your program does not see it, until it encounters a newline character, which flushes the input buffer (sends it to your program, in this case).

    So, if you type "abc" followed by 'enter', there are four characters in the input buffer.

    Code:
    abc\n
    Now, if you use "getchar()", it takes a single character from the input buffer.

    Code:
    // input buffer contains "abc\n"
    
    int cInput;
    
    cInput = getchar();  // takes the 'a' from the input buffer
    putchar(cInput);     // prints 'a' to the output (screen)
    
    cInput = getchar();  // takes the 'b' from the input buffer
    putchar(cInput);     // prints 'b' to the output (screen)
    
    cInput = getchar();  // takes the 'c' from the input buffer
    putchar(cInput);     // prints 'c' to the output (screen)
    
    cInput = getchar();  // takes the '\n' from the input buffer
    putchar(cInput);     // prints '\n' to the output (screen)
    
    // the input buffer is now empty
    This is why, in that link, we use '\n' to determine when a single line has been entered. The program receives the user input, and each character is plucked, one by one, from the input buffer until the newline (where we know it ends, for that line) is read. (Click_here already covered the other option, EOF.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ validation
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:53 PM
  2. validation
    By chodmama in forum C Programming
    Replies: 3
    Last Post: 02-20-2006, 01:05 AM
  3. validation
    By blanny in forum C Programming
    Replies: 9
    Last Post: 03-05-2004, 07:43 PM
  4. need help in validation
    By dholman in forum C Programming
    Replies: 2
    Last Post: 01-08-2004, 09:27 AM
  5. Help with validation
    By boontune in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2003, 09:44 AM