> So, a colleague recenly worked on a small amount of code that required checking whether a string is a number, therefore, thinking about it and realizing that there is no good solution in C ++
In C ++ 11, you have std::stol and / or std::stod that can do what you need.
Update If you do not want to use exceptions, then strtol(str, &endp) will perform the conversion.
You can check if str == endp after the call; if they are the same, then no conversion was possible (since endp points to the beginning of the uncured part of the line)
Like this:
strtol(str, &endp); if (endp==str) { }
Marshall Clow Nov 28 '12 at 17:05 2012-11-28 17:05
source share