Thread: isdigit help with validation function

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    isdigit help with validation function

    Basically I'm entering an integer like 3, and it's saying its not an int.

    Any help would be greatly appreciated.

    Code:
    #include <iostream>
    #include <ctype.h>
    using namespace std;
    
    
    class IntRange
    {
        private:
           int input;
           int lowest;
           int highest;
    
    
        public:
           IntRange(int, int);
           int getInt();
    };
    
    
    IntRange::IntRange(int lower, int upper)
    {
        lowest = lower;
        highest = upper;
    
    
    }
    
    
    IntRange::getInt()
    {
    
    
        cin >> input;
    
    
        while ((input < lowest) || (input > highest) || (isdigit(input) == 0))
        {
            cout << "That is not a valid input, please enter a number between " << lowest << " and " << highest << endl;
            cin >> input;
        }
    
    
        return input;
    }
    
    
    int main()
    {
        int number;
        cout << "please enter a number" << endl;
    
    
        IntRange validate(1, 4);
    
    
        number = validate.getInt();
    
    
        cout << number;
    
    
    
    
    
    
        return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    isdigit() is meant to be used on an ASCII character. For example, isdigit('3') would return true where as isdigit('f') would return false.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    so I need to pass it a char not a int? Thanks.

  4. #4
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by Zeeth View Post
    so I need to pass it a char not a int? Thanks.
    Yes, but in this case you aren't reading characters directly (you're using an istream object to parse the input), so you just need to check the stream state of 'cin' with istream::fail().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isdigit function help!!
    By rambright in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2010, 05:55 AM
  2. isdigit() Function Again -_-
    By pobri19 in forum C Programming
    Replies: 5
    Last Post: 05-23-2008, 03:14 AM
  3. isdigit() Function!
    By pobri19 in forum C Programming
    Replies: 1
    Last Post: 05-22-2008, 10:57 PM
  4. isdigit function
    By jdavenport in forum C Programming
    Replies: 5
    Last Post: 11-18-2005, 09:26 PM
  5. isdigit() function
    By conor20_ie in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2005, 02:25 PM