Input validation problem

This is a discussion on Input validation problem within the C Programming forums, part of the General Programming Boards category; Hi, Very beginner here. I am trying to capture user input. Correct input would be in the form of the ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    15

    Input validation problem

    Hi,

    Very beginner here. I am trying to capture user input. Correct input would be in the form of the letter F followed by 3 digits. i.e. F103. Or the word 'quit' to move back to a higher menu.

    So far I have been using:
    Code:
    #define FNUML 5
    
    fgets(entry, FNUML, stdin);
    while(getchar() != '\n')
       continue;
    Later I compare the input to the proper format or to 'quit'. That part is working fine.

    Where I have a problem is when the user enters something that is shorter than FNUML. i.e. F12 or ABC. Something that is shorter than 5 characters incl the newline. So fgets() is still waiting for one more input. I figured I could try to write my own version and just go character by character with a getchar() but I was hoping there was a better way.

    It seems I am always spending more time with input validation than with anything else. I was trying to google this but most stuff I came up with discussed other problems. Sorry for a possible double post.

    Thank you!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Huncowboy View Post
    Where I have a problem is when the user enters something that is shorter than FNUML. So fgets() is still waiting for one more input.
    Nope. I ABSOLUTELY PROMISE you that is not how fgets works. The size parameter is just a maximum.

    I am not 100% sure* about the cause of your confusion, but start by taking a look at this:

    STDIN pitfalls

    There are some thoughts about input validation at the end. More importantly, you should understand the first part (up to "Great. So what do I do about it?"). If you already do, say so, but I suspect your problem relates to this (it is the most common beginner issue on cboard).

    * so let's say 75%
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    15
    I have read it once. But to make sure, I will have to read it again tomorrow when I have recharged my brain cells. Brb after coffee. Thanks for the help!

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    15
    Ok. I have read it, I think I am understanding it. This was also covered in the text book. So I it seems my while loop, which is there to discard input longer than 5 is causing a problem. So the problem is if the input is too short fgets() swallows my \n and getchar() in the while loop is waiting until it sees an \n. But if input is too long this is not the case. Is that right?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char entry[5];
        printf("\nEnter flight number: ");
            fgets(entry, 5, stdin);
            while(getchar() != '\n')
                continue;
    }
    So I guess I could put in a strlen after fgets to see how long the captured string is and if it is shorter than needed I will skip the while loop. Would that work? Is there a more elegant solution?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 12:50 AM
  2. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 02:50 PM
  3. Input validation loop failure
    By MacNilly in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2006, 02:29 AM
  4. c++ strings. input validation
    By Crashgr in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2004, 06:54 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 03:44 PM

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