Number detector...

Actually now I'm confused... Where can I use strtol? And strtod?

Suppose that I have a strange string (from a file etc), then I want to parse it. The hard problem is, how to detect value type of a string number correctly. Integer? Double? Float? Hex? etc...
Here's an example :
1
2
3
4
5
6
7
8
9
10
11
12
13
char *d = "25.555";
char *i = "25";
char *f = "25.55f";
char *h = "0x1A";
////////////////////////
nType = GetType(d);
//Convert...
nType = GetType(i);
//Convert...
nType = GetType(f);
//Convert...
nType = GetType(h);
//Convert... 


How to detect these value types properly?
Eg : Use strtol for INT values, strtod for DOUBLE, atof for FLOAT...
Is there any way which can do this?

Thanks.
Actually, I'm using VS2005 so my compiler doesn't support C++11, sorry. And maybe I'm going to upgrade my compiler soon...

But, I haven't seen any algorithm or function which can solve my hard problem...
This should get you started:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

void what_could_it_be( const std::string& str )
{
   if( str.find('.') != std::string::npos )
      std::cout << "probably a "
                << ( str.find_last_of("Ff") != std::string::npos ? "float\n" : "double\n") ;
   else
   {

        std::cout << "probably " // long long and oct elided for brevity
                  << ( str.find_last_of("lL") != std::string::npos ? "a long" : "an int")
                  << ( str.find_first_of("xX") != std::string::npos ? " (hex)\n" : " (dec)\n") ;
   }
}

int main()
{
    // char *d = "25.555" ; // wrong
    const char* d = "25.555" ;

    what_could_it_be(d) ;
}
Wow, it's so awesome!
TY, JlBorges!
Topic archived. No new replies allowed.