about constructor and switch
learning c++
learning_c@hotmail.com
Thu Sep 9 08:19:00 GMT 2004
Hi, I compiled a short code. But it does not work.
There are something wrong in constructor, substr and switch.
10.11.cpp: In member function `int Expr::eval()':
10.11.cpp:28: error: switch quantity not an integer
#include <iostream>
#include <string>
using namespace std;
class Expr{
int val1, val2, value;
string s1;
public:
Expr(const string);
int eval();
void print();
};
Expr::Expr(const string s){
string::iterator iter1;
for(iter1=s.begin();iter1!=s.end();iter1++){
if(!isdigit(*iter1))
val1=(int)s.substr(s.begin(),iter1-1);
val2=(int)s.substr(iter1+1,s.end());
s1=*iter1;
}
}
int Expr::eval(){
switch(s1){
case "+":
value=val1+val2;
return value;
break;
case "*":
value=val1*val2;
return value;
break;
case "-":
value=val1-val2;
return value;
break;
case "/":
value=val1/val2;
return value;
break;
}
}
int main(){
int output;
Expr input("5+3");
output=input.eval();
cout<<"The result is: "<<output<<endl;
return 0;
}
_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail
More information about the Gcc-help
mailing list