i cant solve this error.Please help.i'm a beginner
[code]
switch(input)
{
case 1:
add=num1+num2+num3;
cout<<"The total answer is:"<<add<<endl;
}
{ case 2:
sub=num1-num2-num3;
cout<<"The total answer is:"<<sub<<endl;
}
{
case 3:
multiple=num1*num2*num3;
cout<<"The total answer is:"<<multiple<<endl;
}
{
case 4:
divide=num1/num2/num3;
cout<<"The total answer is:"<<divide<<endl;
}
return 0;
}
[Error] case label '2' not within a switch statement
[Error] case label '3' not within a switch statement
[Error] case label '4' not within a switch statement
Your case blocks of code should have a break; statement in each one, otherwise all the following blocks are executed also. For instance, your input is "1" so cases 1, 2, 3 and 4 are executed. Input = "2" and cases 2, 3 and 4 are executed.
Unless executing later case blocks of code is what you want. :)
switch (input)
{
case 1:
add = num1 + num2 + num3;
cout << "The total answer is:" << add << endl;
break;
case 2:
sub = num1 - num2 - num3;
cout << "The total answer is:" << sub << endl;
break;
case 3:
multiple = num1 * num2*num3;
cout << "The total answer is:" << multiple << endl;
break;
case 4:
divide = num1 / num2 / num3;
cout << "The total answer is:" << divide << endl;
break;
}
The break; in case 4: isn't needed, there are no later case statements to execute. It doesn't hurt to have if you decide to add other case statements later.