Thread: determine user's input

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    3

    determine user's input

    Hello sir..

    i am a beginner in C++..

    how to determine if the input is not an integer..


    thnx in advance..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    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

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    #include <cctype> has a function for that called isdigit(). Here's an example of it.

    Code:
        
     if(isdigit(a))
            cout << "digit" << endl;
     else
            cout << "character" << endl;
    More information about this header can be further read here:
    cctype (ctype.h) - C++ Reference

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You can also just attempt to input a number and test whether this returns true. Like
    if(cin>>a)

    note: It doesn't really return true. It will just evaluate to true.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    Code:
     
    cin>>input;
    for(int x=0; x<strlen(input); x++)
    { if( (int) input[x] < (int) '0' ||   (int) input[x] > (int) '9' ) )
     { cout<<"Not a numerical input";
     break; }
    
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    like_no_other, you should indent your code properly. It would help if you actually showed the declaration of input (and it looks like your code will be vulnerable to buffer overflow). Chances are, all that casting is unnecessary and the check is more descriptively done with isdigit() anyway.
    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

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    Yes, you're right, it is faulty.He should definately use isdigit() , but I think he could learn more by giving him an explicit way to check his input other than "i_do_all_the_work_for_you" function.
    Code:
    char input[20];
    cin.getline(input, 19, '\n');
    bool IsInt=1;
    for(int x=0; x<strlen(input); x++)
    { if( (int) input[x] < (int) '0' ||   (int) input[x] > (int) '9' ) )
     {  IsInt=0;
     break; }
    
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That would be better written as:
    Code:
    char input[20];
    cin.getline(input, sizeof(input), '\n');
    bool IsInt = true;
    for (size_t x = 0, n = strlen(input); x < n; ++x)
    {
        if (input[x] < '0' || input[x] > '9')
        {
            IsInt = false;
            break;
        }
    }
    EDIT:
    Ah, but I notice that this algorithm does not correctly handle the input of an empty string.
    Last edited by laserlight; 05-08-2009 at 12:14 PM.
    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

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> He should definately use isdigit()

    I disagree. The operator>> used with cin automatically determines if input is an integer or not. There's no need for you to re-implement what was already done.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    242
    Won't just cin.fail() work if the variable is defined as an integer?
    Code:
    int Var;
    bool x;
    
    cin << Var;
    x = cin.fail();
    It seems to me that we now have x == 1 iff the user didn't enter a valid integer. And you need no additional library or anything.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, that's what we're talking about when we say to use if(cin>>a). The return value of cin >> a evaluates to false if an integer was not read in (and the failbit is set).

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    @laserlight:i don't follow, what was wrong with my code?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by like_no_other
    i don't follow, what was wrong with my code?
    • If the input is an empty string, your code would treat it as a valid integer.
    • getline() should have been called with 20 instead of 19 as the second argument (but then 20 was arbitrary in the first place, so this is a minor point).
    • strlen() should only be called once rather than on each iteration.
    • The casts to int are unnecessary.
    • The code needs to be properly and consistently indented.
    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

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> what was wrong with my code?
    Beyond what laserlight stated, your code sets IsInt to false on valid integers and true on invalid integers. True, those cases (negative numbers or overflow) might be rare depending on what you're using it for, but you don't know what the OP is using this for so I'd recommend the built-in capabilities that handle end cases better.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    3
    thnx to all of u guys.. and sori for the late reply... i used "if (cin>>a)" and it works but when i enter any number that has a decimal like 2.35 or 13.43 etc.. it treats as an integer too why? AFAIK ther is no decimal in integer value..

    thnx in advance and sori for my english..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. reading data from the users input
    By SpEkTrE in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2005, 06:15 PM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM