Reputation: 37
and thank you for viewing this post. I need some help for my programming. I used to compile with turbo pascal and now I'm learning C++. Here's the problem.
I want to make a mathematics thing with an input and output something like this.
Input(in pseudocode):
Var a,b:=int; op:=char;
readln(a);
readln(b);
case op of
"+" : write(a+b);
"-" : write(a-b);
">" : if a>b then write("true"); else write("false");
I've tried to do it in C++ like this but it failed to be compiled. I don't put include here because i don't really understand why is it become bold. I wrote it something like this.
void main()
{
int a,b,c;
char op;
std::cin>>a>>op>>b;
switch (c){
case (std::cin>>op == "+") :{
c=a+b;
std::cout<<c<<std::endl;
}
}
}
this is the error
1>test03.cpp(10): error C2051: case expression not constant
1>test03.cpp(14): warning C4060: switch statement contains no 'case' or 'default' labels
can anyone help me? thanks.
Upvotes: 0
Views: 189
Reputation: 446
You are not too far off the mark. The compiler is telling you exactly what the problem is. You must use a constant in your case expression.
You have a few other problems in there too and you have no error handling.
But to help you get started, try this ...
void main()
{
int a, b, c;
char op;
std::cin >> a >> op >> b;
switch(op){
case '+':
c = a + b;
break;
}
std::cout << c << std::endl;
}
Upvotes: 1
Reputation:
You don't assign any value to c, it contains garbage when you want to use it in the switch block.
I think You wanted to write
switch(op)
and
case '+'
Upvotes: 3