Displaying a simple menu in console using C program with while-do constructs
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
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 (/TC)
Other info: none
To do: Displaying a simple menu using do-while construct in C program
To show: Another do-while statement C program example
// Another do-while statement example
#include <stdio.h>
// Prototype required by standard
int get_menu_choice(void);
void main(void)
{
int choice;
// Call get_menu_choice() function
choice = get_menu_choice();
printf("You have chosen Menu #%d\n", choice);
printf("\n");
}
int get_menu_choice(void)
{
int selection = 0;
do
{
printf("1 - Add a record");
printf("\n2 - Change a record");
printf("\n3 - Delete a record");
printf("\n4 - Quit");
printf("\nEnter a selection: ");
/* scanf("%d", &selection ); */
scanf_s("%d", &selection, 1);
} while ((selection < 1) || (selection > 4));
return selection;
}
Output example:
1 - Add a record
2 - Change a record
3 - Delete a record
4 - Quit
Enter a selection: 1
You have chosen Menu #1
Press any key to continue . . .
1 - Add a record
2 - Change a record
3 - Delete a record
4 - Quit
Enter a selection: 2
You have chosen Menu #2
Press any key to continue . . .
1 - Add a record
2 - Change a record
3 - Delete a record
4 - Quit
Enter a selection: 2
You have chosen Menu #2
Press any key to continue . . .
1 - Add a record
2 - Change a record
3 - Delete a record
4 - Quit
Enter a selection: 4
You have chosen Menu #4
Press any key to continue . . .