Hi, I am new here.
I have a problem with my switch. I was investigating it on the net but I really haven't got the solution yet.
I always get these error- messages:
14 error: 'b' cannot appear in a constant-expression
18 error: 'c' cannot appear in a constant-expression
9 warning: unused variable 'b'
10 warning: unused variable 'c'
#include <iostream>
usingnamespace std;
int main()
{
int a=23;
int b=22;
int c=23;
switch (a)
{
case b:
cout<<" this is b";
break;
case c:
cout<<" this is c";
break;
default:
cout<<"non of them is good";
break;
}
cin.get();
}
Why it does not let me run the program? Since it is a basic scholar example!
Thank you for the answer in advance.
The error says it all. The cases must be constant integral expressions (constants known at compile-time).
If you make b and c const, it'll work (at least in this example).
Since it is a basic scholar example!
...which is hopefully supposed to demonstrate that you can't do this with switch.
If not, one has to seriously doubt the competence of whoever contrived this "scholar example".
#include <iostream>
usingnamespace std;
int main()
{
int a=23;
constint b=27;
constint c=23;
switch (a)
{
case b:
cout<<" this is b";
break;
case c:
cout<<" this is c";
break;
default:
cout<<"none of them is good";
break;
}
cin.get();
}