Listing a menu selection using do-while loop C++ program example

 

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: Listing a simple menu selection using the do-while loop in C++ programming

To show: How to print a list of menu using the do-while loop in C++ programming

 

// program to illustrate the do-while loop in C++

#include <iostream>

using namespace std;

 

int main(void)

{

int selection;

 

do

{

cout<<"\n Menu (1,2 or 3, others to exit)"<<"\n";

cout<<"\n 0thers: Exit";

cout<<"\n 1: Append";

cout<<"\n 2: Delete";

cout<<"\n 3: Modify";

cout<<"\n\n Enter selection: ";

cin>>selection;

}while((selection > 0) && (selection < 4));

// true for 1, 2 and 3 ONLY, then repeat

// false for other numbers including 0, then stop.

// The do loop is repeated if the while expression is true.

 

return 0;

}

 

Output examples:

 

Menu (1,2 or 3, others to exit)

 

0thers: Exit

1: Append

2: Delete

3: Modify

 

Enter selection: 3

 

Menu (1,2 or 3, others to exit)

 

0thers: Exit

1: Append

2: Delete

3: Modify

 

Enter selection: 1

 

Menu (1,2 or 3, others to exit)

 

0thers: Exit

1: Append

2: Delete

3: Modify

 

Enter selection: 2

 

Menu (1,2 or 3, others to exit)

 

0thers: Exit

1: Append

2: Delete

3: Modify

 

Enter selection: 7

 

Press any key to continue . . .

 

 

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