Smart conversion from a string to different type of numbers

D

d.avitabile

I have a C++ code that is reading a list of parameters from a file.

PARAMETERS stringParam="stringValue", intParam=4, doubleParam =
3.533, ... END

The values can be strings as well as integers or doubles, and I don't
know what they will be ( I don't know how the parameter list will look
like) . At the moment I have a mechanism that select all the values
and read them into C++ strings, regardless of their type.

What I need is a function that converts smartly my strings into
numbers: for instance, if I fed it with "4", it would return me the
type "integer" and store the value 4 accordingly, whereas if I fed it
with a "3.533" it would return the type "double" and store the value
3.533.

Any idea as to something like that exists already?

Thanks in advance

Daniele
 
C

Charlton Wilbur

d> I have a C++ code

.....which you asked about in comp.lang.c.

comp.lang.c++ is down the hall. Good luck.

Charlton
 
E

Eric Sosman

I have a C++ code that is reading a list of parameters from a file.

PARAMETERS stringParam="stringValue", intParam=4, doubleParam =
3.533, ... END

The values can be strings as well as integers or doubles, and I don't
know what they will be ( I don't know how the parameter list will look
like) . At the moment I have a mechanism that select all the values
and read them into C++ strings, regardless of their type.

What I need is a function that converts smartly my strings into
numbers: for instance, if I fed it with "4", it would return me the
type "integer" and store the value 4 accordingly, whereas if I fed it
with a "3.533" it would return the type "double" and store the value
3.533.

If you want a C++ solution, try a C++ newsgroup.

In C, I'd approach the problem by using a struct
and a union, something like:

struct number {
enum { ERROR, LONG, DOUBLE } type;
union {
long l;
double d;
} value;
};

.... with a function that first tries to convert the string
with strtol(), and if that doesn't work tries again with
strtod(), and gives up if neither works:

#include <stdlib.h>

struct number
getNumber(const char *string)
{
struct number n;
char *endp;

n.value.l = strtol(string, &endp, 10);
if (endp != string && *endp == '\0') {
n.type = LONG;
return n;
}

n.value.d = strtod(string, &endp);
if (endp != string && *endp == '\0') {
n.type = DOUBLE;
return n;
}

n.type = ERROR;
return n;
}

A C++ addict might take a different approach.
 
M

Martin Ambuhl

I have a C++ code that is reading a list of parameters from a file.

Bummer. The C++ language is not any more topical in comp.lang.c than
are COBOL, LISP, or Fortran. My opinion of that bloated language
designed for obfuscation will be left unsaid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top