Errors 2043, 2046 and 2047

I keep getting error messages, quoting errors 2043, 2046 repeatedly and then 2047 for the one default at the end. How can I prevent these?

Here is the code:

#include <iostream>

using namespace std;

int main ()
{
int x;

cout<< "Choose a number"<<endl;
cin>>x;

char o;
cout<< "Choose whether you want to multiply (*) divide (/) add (+) or subtract (-)this number"<<endl;
cin>>o;

switch(o);
{
case '+':
int y;
cout<<"What would you like to add?"<<endl;
cin>>y;
cout<<x<< " plus " <<y<< " is " <<x + y<<endl;
}
break;
{
case '-':
int y;
cout<<"What would you like to subtract?"<<endl;
cin>>y;
cout<<x<< " minus " <<y<< " is " <<x - y<<endl;
}
break;
{
case '*':
int y;
cout<<"What would you like to multiply?"<<endl;
cin>>y;
cout<<x<< " times " <<y<< " is " <<x * y<<endl;
}
break;
{
case '/':
int y;
cout<<"What would you like to divide?"<<endl;
cin>>y;
cout<<x<< " divided by " <<y<< " is " <<x / y<<endl;
}

default:
cout<<"You did not input of the the required operations"<<endl;
break;

return 0;
}
_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Here are the error messages:

1>------ Build started: Project: Tests, Configuration: Release Win32 ------
1> Tests.cpp
1>Tests.cpp(16): warning C4060: switch statement contains no 'case' or 'default' labels
1>Tests.cpp(18): error C2046: illegal case
1>Tests.cpp(24): error C2043: illegal break
1>Tests.cpp(26): error C2046: illegal case
1>Tests.cpp(32): error C2043: illegal break
1>Tests.cpp(34): error C2046: illegal case
1>Tests.cpp(40): error C2043: illegal break
1>Tests.cpp(42): error C2046: illegal case
1>Tests.cpp(49): error C2047: illegal default
1>Tests.cpp(51): error C2043: illegal break
Last edited on
You must not close the curly bracket after each case. Check the syntax of the switch construct...
Actually, it looks like TC is purposefully trying to create scopes for each case so variables can be declared.

TC: You need to place the opening curly brace inside the case statement as well.
Right, I missed the scopes idea.

@ OP: the problem is that the first right curly bracket ends the entire switch, then the following cases are just inconsistent. So you can either remove all the curly brackets (and give up the use of the scopes) or follow Zhuge's advice.
Check your switch(o);. There should be a big ass hint staring at you right in your face ;)

Also as for the placement of your curly braces, what I prefer to do is, if any one of my cases have more than one line of code underneath, I put the entire body inside braces. That way, the compiler won't mess up the scope. And yes, that includes the break; which I would always put inside the cases. That way I don't get confused as to which one goes with which.
Topic archived. No new replies allowed.