# Library Function to verify Int

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-29-2005
Mr_roboto
Library Function to verify Int
I have a function that will only work with integers. Is there a library function that will check user input to make sure that the data entered is integers.

[EDIT]
Currently I am using a bool function that uses 'isalpha()' returning true and false and I wondering if there is something simpler
[\EDIT]

Thanks.
• 09-29-2005
sand_man
isdigit()?

Code:

`if(isdigit(ch) && ch > INT_MIN && ch < INT_MAX)`
• 09-29-2005
Stoned_Coder
• 09-29-2005
Ancient Dragon
If you want a function to validate user input while typing and ignore non-digit keyboard entries, then no, there is no standard function to do that. You have to write your own. isalpha() will not tell you whether a character is a digit or not -- use isdigit() for that. isalpha() indicates that a character is alphabetic ('a'-'z' or 'A' - 'Z'), and those are 52 out of possible 255. So when isalpha() fails doesn't mean that the character is a numeric digit.
• 09-29-2005
Daved
Another easy way to check for an int that works better than isdigit is to create a stringstream and attempt to extract the int from the stringstream. If it fails or has leftover characters then it is not an int.
• 09-29-2005
Mr_roboto
Thank you
Awsome tips guys.

Thank you.
• 09-29-2005
Ancient Dragon
Quote:

Originally Posted by Daved
Another easy way to check for an int that works better than isdigit is to create a stringstream and attempt to extract the int from the stringstream. If it fails or has leftover characters then it is not an int.

I haven't used stringstream very often -- how can you tell that the string contained non-decimal characters? For example " 12ab34". atoi() just returns 12, and stringstream does the same. So, without checking the number of digits in the integer against the length of the original string, how do you know stringstream could not convert all the characters? I know the fail() will return true if none of the string can be converted -- such as "a123".
• 09-29-2005
Daved
With the stringstream you just check if there is anything left in the stream. I don't know the best way to do this. After extracting into the int, you could extract into a string and see if the string is empty, or maybe you could use get() or peek() to see if there is anything left. Obviously, for "12", the stream will be empty, for "12ab34" it will have "ab34".
• 09-29-2005
ZuK
I've tried this.
Code:

```#include <sstream> #include <iostream> using namespace std; bool isint( const string& s ) {   stringstream str;   string srest;   int i;   str << s;   str >> i;   str >> srest;    if ( srest.length() > 0 )       return false;   str.str("");   str.clear();   str << i;   return str.str() == s; } int main() {   string str("12ad34");     if ( isint( str ) )       cout << str << " is an int" << endl;   else       cout << str << " is not an int" << endl;         str = "123456899999999";     if ( isint( str ) )       cout << str << " is an int" << endl;   else       cout << str << " is not an int" << endl;     str = "123459";   if ( isint( str ) )       cout << str << " is an int" << endl;   else       cout << str << " is not an int" << endl; }```
Without that second check in isint() overflow would not be detected. ( I'm using g++ 3.3.5 )
Kurt
• 09-29-2005
Ancient Dragon
This is still a lot easier on the eyes. It doesn't have all that messy stringstream stuff.
Code:

```bool isint( const string& s ) {   for(int i = 0; i < s.length(); i++)   {     if( !isdigit(s[i]))         return false;   }   return true; }```
• 09-29-2005
ZuK
Looks a lot better BUT:
a) Doesn't detect overflow.
b) returns false on negative ints ( ok. my version returns false if there ise a + sign )
Kurt
• 09-29-2005
Stoned_Coder
• 09-29-2005
Ancient Dragon
checking for sign can be easily fixed.
Code:

```bool isint( const string& s ) {   int i = 0;   if( s[0] == '-' || s[0] == '+')     i++;   for(; i < s.length(); i++)   {     if( !isdigit(s[i]))         return false;   }   return true; }```
The way I understand it, the function is supposed to determine if the string contains all digits or not. It doesn't give a hoot about integer overflow, which is a different problem altogether.
• 09-29-2005
Ancient Dragon
Quote:

Originally Posted by Stoned_Coder