Sams Teach Yourself C in 21 Days

(singke) #1
LISTING13.4 menu.c. Using an infinite loop to implement a menu system
1: /* Demonstrates using an infinite loop to implement */
2: /* a menu system. */
3: #include <stdio.h>
4: #define DELAY 150000 /* Used in delay loop. */
5:
6: int menu(void);
7: void delay(void);
8:
9: int main( void )
10: {
11: int choice;
12:
13: while (1)
14: {
15:
16: /* Get the user’s selection. */
17;
18: choice = menu();
19:
20: /* Branch based on the input. */
21:
22; if (choice == 1)
23: {
24: puts(“\nExecuting task A.”);
25: delay();
26: }
27: else if (choice == 2)
28: {
29: puts(“\nExecuting task B.”);
30: delay();
31: }
32; else if (choice == 3)
33: {
34: puts(“\nExecuting task C.”);
35: delay();
36: }
37: else if (choice == 4)
38: {
39: puts(“\nExecuting task D.”);
40: delay();
41: }
42: else if (choice == 5) /* Exit program. */
43: {
44: puts(“\nExiting program now...\n”);
45: delay();
46: break;
47: }
48: else

318 Day 13

21 448201x-CH13 8/13/02 11:12 AM Page 318

Free download pdf