A statement at the end of the last case in switch statement

Pages: 12
Hi there!

I've written this switch statement:

1
2
3
4
5
6
7
switch(10)
{
case 0:
	//...
case 1:
	//...
;}


If I don't put ';' at the end of this statement, the compiler (MS Visual 2008) shows the error: "missing ';' before '}'".

Why is that ? :/
Show the actual code. That shouldn't happen unless you have an error somewhere else.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{

	cout << "Hello!";
	int opt;
	cin >> opt;

	switch(opt)
	{
	case 1:
		//...
	case 2:
		//...
	case 3:
		//...
	}

	return 0;
}


Visual's still showing that error :/
What's in those cases? Chances are you're missing a semicolon somewhere else.
tummychow wrote:
What's in those cases?


The point is - there's nothing in those cases :) - so I'm not missing anything...
You're missing a ;
closed account (j3bk4iN6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{

	cout << "Hello!";
	int opt;
	cin >> opt;

	switch(opt)
	{
	case 1:
		//...
	case 2:
		//...
	case 3:
		//...
	};  //right here

	return 0;
}
 


you might also want to create a default: for exceptions
Last edited on
I know that I'm missing it, but there's no problem in case 1 or case 2...

abilify, your code is incorrect...
try this code: I mean seriously try it: copy paste to the editor and compile it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{

	cout << "Hello!";
	int opt;
	cin >> opt;

	switch(opt)
	{
	case 1:  break;
		//...
	case 2:  break;
		//...
	case 3:  break;
		//...
        default: break;

	}

	return 0;


then after that if it works, take out the breaks and see if it will compile again. It should in both cases I would think but it's possibly not...
Last edited on
closed account (j3bk4iN6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int main()
{

	cout << "Hello!";
	int opt;
	cin >> opt;

	switch(opt)
	{
	case 1:  break;
		//...
	case 2:  break;
		//...
	case 3:  break;
		//...
        default: break;

	}; //<---------------------------------------------

	return 0;

 
 
 


 Format:





 
Length:  (max=8192)        
That white space is unecessary and so is the semi-colon on line 21.
As for the answer the compiler MS uses requires a command to be inside the last case.
Last edited on
closed account (j3bk4iN6)
you don't have to break default. sorry about the semi colon. whats the error number. wait your missing the closed braces after return 0;
Last edited on
the compiler MS uses requires a command to be inside the last case.
the version of the loop I posted compiles, however when you delete the break statements it doesn't

error C2143: syntax error : missing ';' before '}'

wait your missing the closed braces after return 0;

stop being silly, I know your trying to help but it was a copy paste error.
Last edited on
closed account (j3bk4iN6)
default doesn't need a break, you can put it in there but you don't need to.
no default doesn't need a break (neither do the others), but the answer to the problem is I have said already the compiler will not compile if there is no command in the last statement of the switch whatever that may be.
Last edited on
closed account (j3bk4iN6)
ive never ran a swich without some type of code in them. default doesn't need a break but the others do or it iterates through all the cases. I'm not only trying to help I'm trying to learn and thats not silly goose
Last edited on
that doesn't mean they have to be in there, what happens if I want all commands in the switch to execute if the case is 1, and only commands 2-n if the case is 2, and 3-n if the case is 3 and so on...?
closed account (j3bk4iN6)
then why are you using a switch to begin with? the switch handles the exceptions you don't have to iterate through each one.
Last edited on
You don't seem to get my point at all.
I'm answering the OP's question:
in the case of:
1
2
3
4
5
6
7
8
9
10
11
switch(opt)
	{
	case 1: break;
		//...
	case 2: break;
		//...
	case 3: break;
		//...
    default: 

	}

the code will not compile.
in the case of:
1
2
3
4
5
6
7
8
9
switch(opt)
	{
	case 1: break;
		//...
	case 2: break;
		//...
	case 3: 

	}


The code does not compile. The op was asking why, the answer is simply: the last case whether that be default, or case 27387237, needs to have a command, function, or break something anything that does something... It CANNOT BE BLANK.
closed account (j3bk4iN6)
you don't have to iterate the switch handles that.
Pages: 12