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:
@ 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.