Thread: Enum within a class

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Unhappy Enum within a class

    Code:
    #include<string>
    #include<sstream>
    #include<iostream>
    using namespace std;
    class token
    {
        std::string ori_val;
        public:
        token(std::string);
        enum optype{oprnd,add,sub,mul,div,sin,cos,tan,log};
        optype opt;
        double num;
    
        char sng_oprt;
        //std::string str_oprt;
    
    };
    token::token(std::string input)
    {
        ori_val=input;
        istringstream in;
        in.str(input);
        char c;
        std::string str;
        double num_in;
        if(in>>c)
        {
            sng_oprt=c;
            switch (c)
            {
                case '+':opt=token::add;break;
                case '-':opt=token::sub;break;
                case '*':opt=token::mul;break;
                case '/':opt=token::div;break;
            }
            in.clear();
        }
        else if(in>>num_in)
        {
            num=num_in;
            opt = token::oprnd;
            in.clear();
        }
        else if(in>>str)
        {
    
            switch (str[0])
            {
                case 's':opt=token::sin;break;
                case 'c':opt=token::cos;break;
                case 't':opt=token::tan;break;
                case 'l':opt=token::log;break;
            }
    
        }
    
    
    
    }
    
    int main()
    {
        token t("sin");
        cout<<t.opt;
        return 0;
    }
    Shouldn't the enum value for sin be 5 ? Instead it is always returning a garbage value. Did I miss anything important about enumeration types?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You know, "if(in>>c)" will always be true if 'input' is more that 1 characters long.
    Devoted my life to programming...

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    o...I thought that giving a whole string to a character input would return false..
    Anyway..after it being true would the character c contain the first letter from the string or a random value?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enum type In class.
    By epidemic in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2007, 10:14 AM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. friends class lil prblm with enum
    By rip1968 in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2002, 09:57 PM

Tags for this Thread