Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 22nd, 2007, 02:35 AM
Jim Michaels
Guest
 
Posts: n/a
Default istream cin multiple types. how detect type?

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.
  #2  
Old April 22nd, 2007, 03:35 AM
David Harmon
Guest
 
Posts: n/a
Default Re: istream cin multiple types. how detect type?

On Sat, 21 Apr 2007 18:25:51 -0700 in comp.lang.c++, Jim Michaels
<jmichae3REMOVE@THISPLEASEyahoo.comwrote,
Quote:
>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."

  #3  
Old April 22nd, 2007, 05:05 AM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: istream cin multiple types. how detect type?

Jim Michaels wrote:
Quote:
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.

  #4  
Old April 22nd, 2007, 10:35 AM
James Kanze
Guest
 
Posts: n/a
Default Re: istream cin multiple types. how detect type?

On Apr 22, 3:25 am, Jim Michaels <jmichae3REM...@THISPLEASEyahoo.com>
wrote:
Quote:
how do I write an overloaded >operator for istream that
Quote:
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)
Quote:
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.
Quote:
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.)

--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  #5  
Old April 22nd, 2007, 10:45 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: istream cin multiple types. how detect type?

"Jim Michaels" <jmichae3REMOVE@THISPLEASEyahoo.comwrote in message
news:WoGdnQ0KRNs_JLfbnZ2dnUVZ_vOlnZ2d@comcast.com. ..
Quote:
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.


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles