I have a string
variable that can be 1 of the 3 things:
- A number
- A number in scientific notation
- Text
In cases 1 and 3 I want to do nothing and pass the data along. But in Case 2 I need to convert it a regular number. If I simply always convert the variable to a regular number then when it contains actual text it becomes "0". So I need to know if the string is a number in scientific notation. The obvious, dirty answer is an algorithm like this:
Iterate through string as long as you see numbers. If the first encountered letter is "e" or "E" and it is followed by either "+" or "-", OR strictly more numbers, then it's a number in scientific notation, otherwise it's just a regular number or text.
But I assume there is a better way to do this in C++98 (without boost). Is there any built-in method to help? Even if it's something that just uses a try/catch.
EDIT The question was closed because it was assumed to be homework. It is not homework. Therefore, it should be reopened. Also, to clarify, I am forced to use C++98 due to technical restraints.
I've drawn a proposed finite state automota based on my initial idea ("other" implies ALL characters not otherwise specified for that given state). I believe it is correct.
Some example inputs that should accept:
1.453e-8
0.05843E5
8.43e6
5.2342E-7
Some example inputs that should fail:
hello
03HX_12
8432
8432E
e-8
fail-83e1