Verifying if a string is a number

Hello all,

Customary warning: I'm a noob

I need to know the most efficient way to check a string and verify that it is a valid number, which means:

All these are allowed: +-,.eE1234567890

However, there are annoying rules about where those characters can be with respect to each other, for instance:
1. commas have to be 3 spaces apart
2. commas, dots and 'E's can't be right next to each other
3. signs can only be first in the string, or after an e
and so on...

For instance, this is ok: 1,000.0

But this sort of thing needs to be rejected:
1,1,1
1.,1
1e,1
,111
1+1
abc

etc...

I have managed to write some code to do this, it is just long and annoying, and looks hideous.

I just wanna know if there is a nice way to do it.

I don't expect anyone to actually write this code and show me, but if you could point me in the right direction, that would help a lot, thank you.

Have you considered using regular expressions?

http://en.cppreference.com/w/cpp/regex

Starting point:
http://www.regular-expressions.info/floatingpoint.html
When I did it a few months ago, I used isdigit

http://www.cplusplus.com/reference/cctype/isdigit/

Its pretty good. You just give it character and it will check whether or not its a digit.
Topic archived. No new replies allowed.