Source of menu.h


  1: // Filename: MENU.H
  2: // Purpose:  Specification file for a Menu class.

  4: /***********************************************************************
  5: This Menu class provides a reasonably useful menuing facility for simple
  6: text-based I/O.  Menus have a title and up to nine (numbered) options
  7: separated from the title, when displayed, by a blank line.  The only
  8: reason the number of options is limited to nine is to avoid the minor
  9: irritant of dealing with two-digit option numbers as opposed to single-
 10: digit option numbers.  Each menu knows what its range of valid options
 11: is, and can get from the user a valid choice in the appropriate range or
 12: return an (error) value of -1 if the user fails to enter a valid option
 13: number during three attempts.
 14: ***********************************************************************/

 16: #ifndef MENU_H
 17: #define MENU_H

 19: class Menu
 20: {
 21: public:

 23:     Menu();
 24:     // Default constructor, which initializes a "blank menu" which,
 25:     // when displayed, simply says that it is a blank menu.
 26:     // Pre:  none
 27:     // Post: numberOfOptions == 0, and first row of "text" contains
 28:     //       "This is currently a blank menu ... "


 31:     Menu(const char title[]);
 32:     // Constructor, which initializes a menu with the title passed in
 33:     // but no options.
 34:     // Pre:  "title" has been initialized.
 35:     // Post: numberOfOptions == 0 and the contents of "title" have
 36:     //       been copied into the first row of "text".


 39:     void AddTitle(const char title[]);
 40:     // Adds a title to the menu (or changes the current title).
 41:     // Pre:  "title" has been initialized.
 42:     // Post: The contents of "title" have been copied into the
 43:     //       first row of "text".


 46:     void AddOption(const char option[]);
 47:     // Adds a new option to the menu and gives it the next available
 48:     // number, unless the menu is "full" (i.e., already contains nine
 49:     // options), in which case an error message has been displayed,
 50:     // followed by the pause message.
 51:     // Pre:  numberOfOptions <= 9, and "option" has been initialized.
 52:     // Post: If numberOfOptions < 9 its value has been incremented by 1.
 53:     //       The resulting value of numberOfOptions, a period, and
 54:     //       and a space have been added to the front of the "option"
 55:     //       string, and this final string has been copied into
 56:     //       text[numberOfOptions].  If numberOfOptions is 9, then
 57:     //       the following two-line message is displayed:
 58:     //       Maximum number of menu options exceeded.
 59:     //       Option: <<text_of_option_goes_here>> not added.


 62:     void Display() const;
 63:     // Displays the menu on the screen.
 64:     // Pre:  The menu has been initialized.
 65:     // Post: The menu has been displayed on the screen, more or less
 66:     //       centered left-to-right and top-to-bottom.  More specifically,
 67:     //       there are two more blank lines at the bottom of the screen
 68:     //       than at the top, and there are two more blank columns on
 69:     //       the right than on the left, giving a slight top-left bias.
 70:     //       A blank menu, or a menu with just a title, however, is
 71:     //       displayed left-justified with only the bias toward the top.


 74:     int Choice() const;
 75:     // Returns a menu choice entered by the user, or a value of -1
 76:     // if the user does not enter a valid menu choice for the current
 77:     // menu during one of three permitted attempts, or if the user
 78:     // attempts to get a choice from a blank menu.
 79:     // Pre:  A menu has been initialized and displayed.
 80:     // Post: Either a valid menu choice for the menu has been returned
 81:     //       or -1 has been returned and both an error message and the
 82:     //       pause message have been displayed.  If three unsuccessful
 83:     //       attempts have been made the displayed message is:
 84:     //       Sorry, but you only get three chances ...
 85:     //       If a choice was sought from a blank menu, the message is:
 86:     //       Menu has no options from which to choose ...


 89: private:

 91:     int numberOfOptions;
 92:     // Number of options currently on the menu.

 94:     char text[1+9][81];  // Includes 1 title row and 9 option rows.
 95:     // Array of strings to hold the menu text, the title in the first
 96:     // row, followed by up to nine options in the remaining nine rows.
 97: };

 99: #endif