istream cin multiple types. how detect type?

J

Jim Michaels

how do I write an overloaded >> operator for istream that

let's say fraction class through set() can take several types:
void set(char*)
void set(long int num, long int den)
void set(double)

and that char* can handle hex strings, binary, and whatever C formatted
numbers you can throw at it, as long as it's a string.

how do I code this?
is could provide me with any number of types, and I don't know how to
detect them so I can call the proper set method.

my best guess is it's something along the lines of...

friend istream& operator>>(istream& is, fraction& f) {
switch(is.type???) {
case int:
char c;
long int num,den;
is>>num>>c>>den;
f.set(num, den);
break;
case double:
double d;
is>>d;
f.set(d);
break;
case string:
string s;
is >> s;
f.set(s); //string automatically converts to char*
break;
default:
std::cerr<<"bad input\n";
break;
}
return is;
}






------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
D

David Harmon

On Sat, 21 Apr 2007 18:25:51 -0700 in comp.lang.c++, Jim Michaels
how do I code this?
is could provide me with any number of types, and I don't know how to
detect them so I can call the proper set method.

my best guess is it's something along the lines of...

friend istream& operator>>(istream& is, fraction& f) {
switch(is.type???) {

There is no way to tell the incoming type without reading (at least the
first part of) it and looking at it.

Read it into a string first, whatever it is. Then see what it looks
like. The easiest way to do that is probably to try to convert it with
strtol() or std::istringstream or whatever, and see if it succeeds. If
nothing succeeds, then handle it as "any other string."
 
G

Gianni Mariani

Jim said:
how do I write an overloaded >> operator for istream that

You need to describe your input character sequence. One way of doing
this is with regular expressions.

e.g.

digit [0-9]
integers {digit}+
sign [+-]?
float_exponent ([eE]{sign}{integers})
float {sign}{integers}?'.'{integers}{float_exponent}

And then push it through a lexical scanner generator that will scan the
input for you or write one from scratch.
 
J

James Kanze

how do I write an overloaded >> operator for istream that
let's say fraction class through set() can take several types:
void set(char*)
void set(long int num, long int den)
void set(double)
and that char* can handle hex strings, binary, and whatever C formatted
numbers you can throw at it, as long as it's a string.
how do I code this?

You'll have to define a format, and read the text for it. Until
you've defined a format, there's not much one can say as to how
to read it.

Note that if you want to support both entering the number in the
form a/b, and as a decimal floating point, you're going to have
to resolve an ambiguity: is the string "42" the a of a/b, or is
it a floating point value? If you want the treatment of "42" to
depend on what follows (a "." or a "/"), you'll have to read the
characters into a temporary buffer, and then use istringstream
to convert this, once you know the format. (Note that if you
want to handle all of the different bases, this will be less
trivial than it might appear.)
 
J

Jim Langston

Jim Michaels said:
how do I write an overloaded >> operator for istream that

let's say fraction class through set() can take several types:
void set(char*)
void set(long int num, long int den)
void set(double)

and that char* can handle hex strings, binary, and whatever C formatted
numbers you can throw at it, as long as it's a string.

how do I code this?
is could provide me with any number of types, and I don't know how to
detect them so I can call the proper set method.

my best guess is it's something along the lines of...
friend istream& operator>>(istream& is, fraction& f) {
switch(is.type???) {
case int:
char c;
long int num,den;
is>>num>>c>>den;
f.set(num, den);
break;
case double:
double d;
is>>d;
f.set(d);
break;
case string:
string s;
is >> s;
f.set(s); //string automatically converts to char*
break;
default:
std::cerr<<"bad input\n";
break;
}
return is;
}

You're on the right track, but instead of a switch you'll have to determine
the format of the string. Consider your binary, for example. If I input
10110101
am I inputing 10,110,101 or 171? Which is why hex starts with 0x
So I guess you could start binary with 0b (I had always wanted a binary type
for constants in C and C++).

So do your checking to determine what type it is then convert it however you
need to.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top