please help

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
Last edited on
Can you post more of your code? etc includes, functions and all that.
And please do keep your code inside "code" and "/code"
Last edited on
closed account (E0p9LyTq)
Your braces do not match, your compiler treats the closing brace at the end of case 1: as the end of your switch statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
}


You can use braces for each case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
}
}
Owh, thanks for the help and guide
problem solved. Thank you Mr furryGuy :)
closed account (E0p9LyTq)
@mister2018,

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.
Last edited on
Topic archived. No new replies allowed.