Enum within a class

This is a discussion on Enum within a class within the C++ Programming forums, part of the General Programming Boards category; 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; ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509

    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?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    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?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 11: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. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM
  5. friends class lil prblm with enum
    By rip1968 in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2002, 10:57 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21