A simple menu selection program using switch-case-break C++ code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Displaying a very simple menu selection using switch-case-break in C++ programming

To show: How to display a very simple menu selection using switch-case-break in C++ programming

 

// the menu selection C++ sample program

#include <iostream>

using namespace std;

 

int main(void)

{

char selection;

 

cout<<"\n Menu";

cout<<"\n========";

cout<<"\n A - Append";

cout<<"\n M - Modify";

cout<<"\n D - Delete";

cout<<"\n X - Exit";

cout<<"\n Enter selection: ";

// read the input

cin>>selection;

 

switch(selection)

{

case 'A' :

case 'a' :{cout<<"\n To append a record\n";}

break;

case 'M' :

case 'm' :{cout<<"\n To modify a record";}

break;

case 'D' :

case 'd' :{cout<<"\n To delete a record";}

break;

case 'X' :

case 'x' :{cout<<"\n To exit the menu";}

break;

// other than A, M, D and X...

default : cout<<"\n Invalid selection";

// no break in the default case

}

cout<<"\n";

 

return 0;

}

 

Output examples:

 

Menu

========

A - Append

M - Modify

D - Delete

X - Exit

Enter selection: a

To append a record

Press any key to continue . . .

 

Menu

========

A - Append

M - Modify

D - Delete

X - Exit

Enter selection: M

To modify a record

Press any key to continue . . .

 

Menu

========

A - Append

M - Modify

D - Delete

X - Exit

Enter selection: d

To delete a record

Press any key to continue . . .

 

Menu

========

A - Append

M - Modify

D - Delete

X - Exit

Enter selection: x

To exit the menu

Press any key to continue . . .

 

Menu

========

A - Append

M - Modify

D - Delete

X - Exit

Enter selection: T

Invalid selection

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index