istream cin multiple types. how detect type?

Discussion in 'C++' started by Jim Michaels, Apr 22, 2007.

  1. Jim Michaels

    Jim Michaels Guest

    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.
     
    Jim Michaels, Apr 22, 2007
    #1
    1. Advertising

  2. Jim Michaels

    David Harmon Guest

    On Sat, 21 Apr 2007 18:25:51 -0700 in comp.lang.c++, Jim Michaels
    <> wrote,
    >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."
     
    David Harmon, Apr 22, 2007
    #2
    1. Advertising

  3. 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.
     
    Gianni Mariani, Apr 22, 2007
    #3
  4. Jim Michaels

    James Kanze Guest

    Re: istream cin multiple types. how detect type?

    On Apr 22, 3:25 am, Jim Michaels <>
    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:
    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
     
    James Kanze, Apr 22, 2007
    #4
  5. Jim Michaels

    Jim Langston Guest

    "Jim Michaels" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Apr 22, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,109
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,183
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,714
    Alex Vinokur
    Mar 6, 2005
  4. ax
    Replies:
    3
    Views:
    485
  5. xmllmx
    Replies:
    5
    Views:
    593
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page