# How can I determine whether a string represents an integer or not?

• 02-05-2003
Noobie
How can I determine whether a string represents an integer or not?
How can I determine whether a string represents an integer or not?
• 02-05-2003
lightatdawn
Well, you could use atoi (Ascii TO Integer) or the like (atof, atol, etc) and determine if the result is a number. atoi returns 0 if the string was not a number. Possibly your string IS the number 0, so just for added safety, in this example we check to make sure that both the value is non-zero and the string does not contain zero.

Code:

```int num; char * string = "12345"; num = atoi( string ); if (num == 0 && string[0] != '0')   printf("Not a Number!"); else   print("Number is: %i", num);```
• 02-05-2003
lightatdawn
... Come to think of it, alternatly you could parse your string for valid integers, either ignoring invalid characters (alphabet, etc) or by simply flagging the string as non-numeric if one is found. i.e.

Code:

```BOOL IsValidNumber(char * string) {   for(int i = 0; i < strlen( string ); i ++)   {       //ASCII value of 0 = 48, 9 = 57. So if value is outside of numeric range then fail       //Checking for negative sign "-" could be added: ASCII value 45.       if (string[i] < 48 || string[i] > 57)         return FALSE;   }   return TRUE; }```
• 02-05-2003
Cela
Or to be more thorough you could check that all of the string is valid in every conceivable way :-)
Code:

```#include <iostream> #include <cstdlib> #include <string> #include <limits> #include <cerrno> using namespace std; bool is_number(string& src) {   const char *start = src.c_str();   errno = 0;   char *end;   long string_value = strtol(start, &end, 10);   if (     errno == ERANGE ||                                // Did we set off errno?     string_value > numeric_limits<int>::max() ||      // Is it too big?     string_value < numeric_limits<int>::min() ||      // Too small?     static_cast<unsigned>(end - start) != src.length() // Non-numerics?     )   {     return false;   }   return true; } int main() {   string s = "12345";   if (is_number(s))   {     cout<<"It's a number!"<<endl;   } }```
• 02-05-2003
Noobie
Im teaching myself c++ and this board is a LOT of help. I looked at the code offered and managed to get the below to work for Borland C++ 5.02. I think this is what I want.

Code:

``` #include <iostream> #include <string> #include <limits> int main() { int num; char string[256]; cin.getline(string, 256, '\n'); num = atoi( string ); if (num == 0 && string[0] != '0') { cout <<"Not a Number!"; } else { cout<<"Number is: "<<num; } return 0; }```
• 02-05-2003
Cela
>>I think this is what I want.
Close enough, if you're teaching yourself C++ then rock solid validation might be more than you want :-)
• 02-05-2003
Noobie
• 02-05-2003
lightatdawn
Celas example is accurate, inclusive, and safe... though a little excessive. ;)

Bravo. Its always nice to see people who want to learn. I'll offer some related information.

Something to look for when processing something like this, is that your char * string is valid. The line "... && string[0] != '0'" will explode if string is not a valid pointer. So checks like:

if (string != NULL) ... //proceed with code

could prove useful. In your particular case that would be useless as string is not a pointer, but something like that would be a smart idea using the IsValidNumber(...) function example.
• 02-05-2003
OneStiffRod
Or u can use the standard lib functions -

#include <locale>

isdigit(); //This fxn determines whether a letter in a string is a numerical value '3' is numerical 'L' is not

I think this used to be in the <cctype.h> header as I remember reading that those checking functions were there but MSDN says it's in the <locale> header. Go figure?
• 02-06-2003
Cela
>>Celas example is accurate, inclusive, and safe... though a little excessive.
There's nothing wrong with being excessive in the right situations, like nuclear reactor or space station controllers :-) But you're right, it's a little much for most normal use