473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Apr 22 '07 #1
4 2694
On Sat, 21 Apr 2007 18:25:51 -0700 in comp.lang.c++, Jim Michaels
<jm************@THISPLEASEyahoo.comwrote,
>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."

Apr 22 '07 #2
Jim Michaels wrote:
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.

Apr 22 '07 #3
On Apr 22, 3:25 am, Jim Michaels <jmichae3REM...@THISPLEASEyahoo.com>
wrote:
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.)

--
James Kanze (Gabi Software) email: ja*********@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

Apr 22 '07 #4
"Jim Michaels" <jm************@THISPLEASEyahoo.comwrote in message
news:Wo******************************@comcast.com. ..
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.
Apr 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Juan Carlos CORUÑA | last post by:
Hello all, I have developed an automation server using Mark's win32 extensions and I must return a IStream COM object from one method of the automation server. Here is an extract of my code:...
15
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
3
by: lpe540 | last post by:
Hi, I'm having trouble using istream to read in a file in its entirety on UNIX. I've written a dummy program that essencially reads in a file from stdin and writes it out to a file. When I cat a...
8
by: Dave Moore | last post by:
Is there any way to specify an istream separator sequence? For example, suppose I have a record consisting of a list of comma-separated values (no whitespace). I want to set the istream up so that...
21
by: Peter Larsen [] | last post by:
Hi, I have a problem using System.Runtime.InteropServices.ComTypes.IStream. Sample using the Read() method: if (IStreamObject != null) { IntPtr readBytes = IntPtr.Zero;...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
3
by: Kourosh | last post by:
Hi all, I'm trying to call a COM function from C# The function gets a paramter of type IStream in C++. I've found the type System.Runtime.InteropServices.ComTypes.IStream, however I'm not sure...
5
by: Peng Yu | last post by:
Hi, Suppose, I have the following code and the requirement is written along with the code. I'm wondering if there is a way to restore the state of an istream. int n, m; std::ifstream...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.