-
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?
-
You know, "if(in>>c)" will always be true if 'input' is more that 1 characters long.
-
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?