I've had quite a bit of trouble trying to write a function that checks if a string is a number. For a game I am writing I just need to check if a line from the file I am reading is a number or not (I will know if it is a parameter this way). I wrote the below function which I believe was working smoothly (or I accidentally edited to stop it or I'm schizophrenic or Windows is schizophrenic):
bool isParam (string line)
{
if (isdigit(atoi(line.c_str())))
return true;
return false;
}
A solution based on a comment by kbjorklu is:
As with David Rector's answer it is not robust to strings with multiple dots or minus signs, but you can remove those characters to just check for integers.
However, I am partial to a solution, based on Ben Voigt's solution, using
strtod
in cstdlib to look decimal values, scientific/engineering notation, hexidecimal notation (C++11), or even INF/INFINITY/NAN (C++11) is:Brendan this
is almost ok.
assuming any string starting with 0 is a number, Just add a check for this case
ofc "123hello" will return true like Tony D noted.
I've found the following code to be the most robust (c++11). It catches both integers and floats.
Few months ago, I implemented a way to determine if any string is integer, hexadecimal or double.
Then in your program you can easily convert the number in function its type if you do the following,
You can realise that the function will return a 0 if the number wasn't detected. The 0 it can be treated as false (like boolean).
With C++11 compiler, for non-negative integers I would use something like this (note the
::
instead ofstd::
):http://ideone.com/OjVJWh
how it works: the stringstream >> overload can convert strings to various arithmetic types it does this by reading characters sequentially from the stringstream (ss in this case) until it runs out of characters OR the next character does not meet the criteria to be stored into the destination variable type.
example1:
example2:
example3:
the "garbage" variable explanation":
why not just check if extraction into my double has a valid value and then return true if it does?
notice example3 above will still successfully read the number 11 into the my_number variable even if the input string is "11ABCD" (which is not a number).
to handle this case we can do another extraction into a string variable(which I named garbage) which can read anything that may have been left over in the string buffer after the initial extraction into the variable of type double. If anything is left over it will be read into "garbage" which means the full string passed in was not a number (it just begins with one). in this which case we'd want to return false;
the prepended "0" explanation":
attempting to extract a single character into a double will fail(returning 0 into our double) but will still move the string buffer position to after the character. In this case our garbage read will be empty which would cause the function to incorrectly return true. to get around this I prepended a 0 to the string so that if for example the string passed in was "a" it gets changed to "0a" so that the 0 will be extracted into the double and "a" gets extracted into garbage.
prepending a 0 will not affect the value of the number so the number will still be correctly extracted into our double variable.