Validating Input.

This is a discussion on Validating Input. within the C Programming forums, part of the General Programming Boards category; I would like to take in an input from the user that's supposed to be a number. I want to ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Validating Input.

    I would like to take in an input from the user that's supposed to be a number. I want to validate the input, and if it's not a number prompt the user to try again. If it is a number, I want to continue.

    I'm thinking I will need to take the input as a string, and check to make sure it is only one numerical digit. If it is, I can convert it to an integer. My question is this: How do I check to see if it's longer than one character, and whether or not it is a number. For example, if the output is "enter any number: " and the user types in "what?" I would like to prompt them to try again.

    I've done a Google search for "string parsing C" and the results showed me how to use a delimiter to break a string up, and maybe there is a way to use strtok to break off just the first character and discard the rest, but I didn't see anything showing me that.

    Any help is greatly appreciated. Thanks!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    Quote Originally Posted by CrunchyMarvin View Post
    I've done a Google search for "string parsing C" and the results showed me how to use a delimiter to break a string up, and maybe there is a way to use strtok to break off just the first character and discard the rest, but I didn't see anything showing me that.
    String parsing is a broad topic, and most of the time when people talk about it it usually means breaking up a list of data points. Doing that might involve strtok(). Turning a string into an integer might be part of it, but not necessarily. And breaking up the long string is usually the part that people have trouble with. Just so you know. It is nice that you tried.

    This page is probably more helpful. FAQ > How do I get a number from the user (C) - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Thank you for the quick reply! I'll read through that link.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    Bay Area, CA
    Posts
    53
    If you are allowed to use scanf, you could just use that. Remember that scanf returns a value, and that value is the number of items that were successfully filled. So if you want to get one integer from the user (you can check to see if it's a single digit after the scanf itself succeeds), and they enter an integer, then scanf will return 1. If they enter a character, scanf will return 0. Depending on if 1 or 0 is returned, you can go from there (if single digit, proceed to next step, if not single digit or if return is 0, try again).

    Otherwise, whiteflags link works for the string parsing route.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by Sorin View Post
    If you are allowed to use scanf, you could just use that. Remember that scanf returns a value, and that value is the number of items that were successfully filled. So if you want to get one integer from the user (you can check to see if it's a single digit after the scanf itself succeeds), and they enter an integer, then scanf will return 1. If they enter a character, scanf will return 0. Depending on if 1 or 0 is returned, you can go from there (if single digit, proceed to next step, if not single digit or if return is 0, try again).

    Otherwise, whiteflags link works for the string parsing route.
    I don't understand what you're saying. My plan is to use scanf("%s", &string[]); how could I use that to see if it's a single integer?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Quote Originally Posted by CrunchyMarvin
    My plan is to use scanf("%s", &string[]); how could I use that to see if it's a single integer?
    If you want to do that, then you need to parse the string, e.g., using strtol, checking for an unsuccessful parse. Otherwise, you could get scanf to parse the input for you, e.g., with %d instead of %s, which is probably what Sorin had in mind.

    Oh, and if you do use %s, then you should specify the field width to avoid buffer overflow.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    Bay Area, CA
    Posts
    53
    Yes, the basic idea was:

    Code:
        int success = 0;
        int number  = 0;
    
        while(success != 1)
        {
            printf("Enter a number: ");
            success = scanf("%d", &number);
            //flush input buffer here in case they input characters
        }
    If success is 1, they entered a number. If it is 0, they didn't. Modify as necessary to check for if the number is single digit or not.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Oh ok. I see what you're saying now. That seems like a much, much easier way than parsing and converting a string. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with validating an input.
    By morrjame in forum C Programming
    Replies: 9
    Last Post: 12-20-2010, 08:55 PM
  2. validating input
    By c-rookie in forum C Programming
    Replies: 15
    Last Post: 08-23-2003, 07:55 PM
  3. Validating an input
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-22-2003, 11:25 PM
  4. Validating input
    By Barjor in forum C# Programming
    Replies: 7
    Last Post: 02-27-2003, 08:42 PM
  5. validating user input
    By andrew_lillis in forum C++ Programming
    Replies: 10
    Last Post: 10-27-2002, 07:11 AM

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