How can I know whether my C++ string variable is a number or not -


I have a string of class string

  string str;  

How can I check whether it is a number or not, there are only 3 possible types of type in strings like

  abcd  < 

Pre> 456)

Note the parenthesis at the end of "str", the only possible combination of numbers and no number

where two are considered below Valid numbers, I know that I can only use lexical_cast if only are already two cases, but about how to consider all three possible cases? I do not have to do some fancy with string, I need to know if it is a valid number as I have described

The C ++ solution is the string stream manually to parse the string. Place your string in a std :: istringstream and read it from.

What you can do to parse it, it is to try to read one ( unsigned ) int from the string.
If it fails, then it is a string that does not start with the digits.
If it works, peek at the next character. If this is a . If you have a floating point number, if this is a ), then you have an integer number. (Otherwise you have a reading error.)

  Read Wide (const std :: string and str) {std :: istringstream iss (str); Int i; If there is a '456' break in ("(! (Iss! (I!))" // "in ARR" ABCD "} and {switch (ispike ()) {case '}': // I; matter '.' {Double D; If (! (Iss> D;)) "Tie!"; D + = i; // D has a floating point value break; Default: "What?" Throw away; } // ...}  

Does this mean?


Comments