home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list13_4.c < prev    next >
C/C++ Source or Header  |  1993-10-16  |  2KB  |  77 lines

  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.  main()
  10.  {
  11.      int choice;
  12.  
  13.      while (1)
  14.      {
  15.  
  16.      /* Get the user's selection. */
  17.      choice = menu();
  18.  
  19.      /* Branch based on the input. */
  20.  
  21.      if (choice == 1)
  22.     {
  23.          puts("\nExecuting choice 1.");
  24.          delay();
  25.      }
  26.      else if (choice == 2)
  27.           {
  28.               puts("\nExecuting choice 2.");
  29.               delay();
  30.       }
  31.      else if (choice == 3)
  32.           {
  33.               puts("\nExecuting choice 3.");
  34.               delay();
  35.           }
  36.      else if (choice == 4)
  37.           {
  38.               puts("\nExecuting choice 4.");
  39.               delay();
  40.           }
  41.      else if (choice == 5)       /* Exit program. */
  42.           {
  43.               puts("Exiting program now...");
  44.               delay();
  45.               break;
  46.           }
  47.           else
  48.           {
  49.               puts("Invalid choice, try again.");
  50.               delay();
  51.           }
  52.      }
  53.  }
  54.  
  55.  int menu(void)
  56.  /* Displays a menu and inputs user's selection. */
  57.  {
  58.      int reply;
  59.  
  60.      puts("\nEnter 1 for task A.");
  61.      puts("Enter 2 for task B.");
  62.      puts("Enter 3 for task C");
  63.      puts("Enter 4 for task D.");
  64.      puts("Enter 5 to exit program.");
  65.  
  66.      scanf("%d", &reply);
  67.  
  68.      return reply;
  69.  }
  70.  
  71.  void delay( void )
  72.  {
  73.      long x;
  74.      for ( x = 0; x < DELAY; x++ )
  75.          ;
  76.  }
  77.