switch in main?

B

bob

I have a switch in a main function:

int main() {

switch ( 5 ) {

case 1 :
// Process for test = 1
//...
break;

case 5 :
// Process for test = 5
//...
break;

default :
// Process for all other cases.
//...

}

return 0;
}

But when I compile I get:

error: expected primary-expression before '}' token
error: expected `;' before '}' token

Is it illegal to have a switch in a main function?
 
J

Jim Langston

bob said:
I have a switch in a main function:

int main() {

switch ( 5 ) {

case 1 :
// Process for test = 1
//...
break;

case 5 :
// Process for test = 5
//...
break;

default :
// Process for all other cases.
//...

}

return 0;
}

But when I compile I get:

error: expected primary-expression before '}' token
error: expected `;' before '}' token

Is it illegal to have a switch in a main function?

Yes. Your compiler is acually bitching about:

default: }

You have no statement after the break: statement, and it was expecting one.
Change it to:

default:
// Process for all other cases.
// ...
break;
}

and it should compile.

In face even an empty statment would work.

default:
;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top