# Thread: Library Function to verify Int

1. ## 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.

2. isdigit()?

Code:
`if(isdigit(ch) && ch > INT_MIN && ch < INT_MAX)`

3. 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.

4. 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.

5. ## Thank you

Awsome tips guys.

Thank you.

6. 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".

7. 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".

8. 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

9. 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;
}```

10. 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

11. c) fails on hexadecimal

12. 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.

13. Originally Posted by Stoned_Coder
c) fails on hexadecimal
Of course it will -- hex is not an integer string of base 10!

14. yes it is but its in base 16. its a valid int. oxff is 255 for example and can be passed to a func requiring an int.

Popular pages Recent additions