Read in enum types

Discussion in 'C++' started by Alex, Jan 16, 2007.

  1. Alex

    Alex Guest

    Hi,

    I want to read in enum types from standard input. Unfortunately it does not
    work. The code is given as follows:

    #include<iostream>
    #include<string>
    #include<fstream>

    using namespace std;

    enum TYP { eins, zwei, drei};

    int main()
    {
        string input("datIn");
        ifstream IN(input.c_str());
        TYP t;

        cin >> t;  // Fehlermeldung

        return 0;
    }

    Do I have to define an input operator ?

    Thank you for your help.
     
    Alex, Jan 16, 2007
    #1
    1. Advertising

  2. Alex

    dasjotre Guest

    Alex wrote:
    > Hi,
    >
    > I want to read in enum types from standard input. Unfortunately it does not
    > work. The code is given as follows:
    >
    > #include<iostream>
    > #include<string>
    > #include<fstream>
    >
    > using namespace std;
    >
    > enum TYP { eins, zwei, drei};
    >
    > int main()
    > {
    > string input("datIn");
    > ifstream IN(input.c_str());
    > TYP t;
    >
    > cin >> t; // Fehlermeldung
    >
    > return 0;
    > }
    >
    > Do I have to define an input operator ?
    >


    You could read an int and convert it to TYP
     
    dasjotre, Jan 16, 2007
    #2
    1. Advertising

  3. Alex wrote:
    > Hi,
    >
    > I want to read in enum types from standard input. Unfortunately it does not
    > work. The code is given as follows:
    >
    > #include<iostream>
    > #include<string>
    > #include<fstream>
    >
    > using namespace std;
    >
    > enum TYP { eins, zwei, drei};
    >
    > int main()
    > {
    > string input("datIn");
    > ifstream IN(input.c_str());


    As a side note, you can directly do this, instead of above 2 lines
    ifstream IN("datIn);

    > TYP t;
    >
    > cin >> t; // Fehlermeldung
    >
    > return 0;
    > }
    >
    > Do I have to define an input operator ?
    >
    > Thank you for your help.
    >
     
    Haro Panosyan, Jan 16, 2007
    #3
  4. Alex

    Ondra Holub Guest

    Alex napsal:
    > Hi,
    >
    > I want to read in enum types from standard input. Unfortunately it does not
    > work. The code is given as follows:
    >
    > #include<iostream>
    > #include<string>
    > #include<fstream>
    >
    > using namespace std;
    >
    > enum TYP { eins, zwei, drei};
    >
    > int main()
    > {
    > string input("datIn");
    > ifstream IN(input.c_str());
    > TYP t;
    >
    > cin >> t; // Fehlermeldung
    >
    > return 0;
    > }
    >
    > Do I have to define an input operator ?
    >
    > Thank you for your help.


    It depends on format in which is your enum written in the stream.
    Anyway you should overload input operator>>:

    template<typename T, typename TRAITS>
    std::basic_istream<T, TRAITS>& operator>>(std::basic_istream<T,
    TRAITS>& is, TYP& data)
    {
    // Here read your type from is any way you want.
    // One possible (of many) example follows:

    std::basic_string<T> s;
    is >> s;

    // Here would be better some searching mechanism or table for
    bigger enums
    if (s == "eins") data = eins;
    else if (s == "zwei") data = zwei;
    else if (s == "drei") data = drei;
    else
    {
    // Wrong input - do what ever you want, but you should switch
    is into bad state
    }

    return is;
    }

    It is written from scratch, so there may be some typos.
     
    Ondra Holub, Jan 16, 2007
    #4
  5. Alex

    Alex Guest

    Thank you for your suggestions. I'll try them out.
     
    Alex, Jan 18, 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. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    581
  2. Jerminia
    Replies:
    3
    Views:
    653
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    494
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    447
    Dave Thompson
    Jun 10, 2004
  5. Randy
    Replies:
    1
    Views:
    527
    David Harmon
    Jan 7, 2006
Loading...

Share This Page