Switches

In my code I have:

1
2
3
4
5
switch( choice )
{
case 0: Chapter1( );
case 1: Chapter2( );
}


It works fine if I select chapter 2, however if I select chapter 1 the program runs the chapter 1 function and then immediately runs the chapter 2 function. Why?
The switch statement in C++ behaves differently than similar constructs in most other languages, in that when the code in one case finishes, execution continues with the following case statement. To prevent this, use the break statement which exits the switch.

1
2
3
4
5
6
7
switch( choice )
{
   case 0: Chapter1( );
      break;
   case 1: Chapter2( );
      break;
}


For documentation of the switch statement see:

http://cplusplus.com/doc/tutorial%20%20/control/

Scroll down for the switch control statement.
Thank you, my program is working as intended now :)
Topic archived. No new replies allowed.