Thread: Input Validation Function

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    13

    Input Validation Function

    Hello,

    So I am having trouble doing a pretty simple input validation function that checks for numeric input.

    Code:
    #include <stdio.h>
    
    int check_hour(void);
    
    int main(void)
    {
        int hour;
    
        printf("Enter the hour: ");
        hour = check_hour();
        printf("The hour is %d\n", hour);
    
        return 0;
    }
    
    int check_hour(void)
    {
        int input;
    
        while (scanf("%d", &input) == 1 && (input < 1 || input > 12))
            printf("Enter a numerical hour between 1 and 12: ");
    
        return input;
    }
    Basically if I input an invalid numerical value that is not between 1 and 12 then I must re-enter the value. However, if the user enters a non-numerical character my function just terminates. I want my function so if the user enters a letter it will re-prompt the user of the invalid input and then ask for a numerical value between 1 and 12.

    Any help or suggestions as to how I can make my function do this would be very much appreciated! Thanks.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,656
    Make your check_hour() function take a character array of size two.
    Then check if both the values are between 0 and 9 included.
    Then convert it into an int, check if it is within your range and return.
    Last edited by manasij7479; 07-08-2011 at 12:20 AM.

  3. #3
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    edit: misread!

    See above post.
    Last edited by \007; 07-08-2011 at 12:16 AM.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    13
    Ya I noticed what I did, I changed my function so if I enter in a letter it tells the user of bad input but now if I enter in integer that is less than 1 or more than 12 the program just terminates.

    Code:
    int check_hour(void)
    {
        int input = 0;
        char ch;
    
        while (scanf("%d", &input) != 1 && (input < 1 || input > 12))
        {
            while((ch = getchar()) != '\n')
                ;
            printf("Enter a numerical hour between 1 and 12: ");
        }
    
        return input;
    }
    I don't know, why this is giving me so much trouble is beyond me. My textbook isn't helping me too much

    Is it not proper code standard to include numerical boundaries in the while loop that has the scanf() function?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Examine your loop condition carefully. For example, why do you write scanf("%d", &input) != 1 instead of scanf("%d", &input) == 1? Do you apply the same idea to (input < 1 || input > 12), or did you mix something up?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    13
    Well basically what I am intending to do is if the user doesn't input a numerical value and the value is not between 1-12 then it tells the user of invalid input and to re-enter.

    Am I approaching this totally wrong? I am still very new to c programming, so excuse my dumb questions lol
    Last edited by stevenson; 07-08-2011 at 12:54 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Quote Originally Posted by stevenson
    Am I approaching this totally wrong?
    No, you're on the right track. You just need to consider your input and step through your loop.
    Last edited by laserlight; 07-08-2011 at 01:29 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    13
    In my while loop I just changed && to || and now everything works, hooray! Now I can continue making my original program Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input/Validation
    By nokia123456 in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 11:26 AM
  2. input validation, kinda
    By dynamethod in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2008, 12:52 AM
  3. Input Validation!
    By noaksey in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 05:19 AM
  4. input validation - new to programming
    By bigzeppelin2k in forum C Programming
    Replies: 2
    Last Post: 10-31-2003, 06:44 PM
  5. Input validation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2001, 11:18 PM