Utilities Package
Version Linux2023.0
|
For displaying menus and getting user menu choices in console applications. More...
#include <utilities.h>
Public Member Functions | |
Menu () | |
Default constructor; creates a "blank" menu. More... | |
Menu (const string &menuTitle) | |
void | setTitle (const string &menuTitle) |
Sets (or resets) the menu title. More... | |
void | addOption (const string &option) |
void | display () const |
int | getChoice (int maxNumTries=3, string userPrompt="Enter the number of your menu choice here and press Enter: ") const |
For displaying menus and getting user menu choices in console applications.
The Menu
class provides a convenient way for simple text-based console programs to display for the user a menu of choices. A menu has a title and up to 20 numbered options which are separated from the title by a blank line when the menu is displayed. A menu knows what its range of valid options is, and can get from the user a valid choice from that range, or return an error value of -1 if the user fails to enter a valid option number during three attempts (by default), or during some client-chosen number of attempts.
Scobey::Menu::Menu | ( | ) |
Default constructor; creates a "blank" menu.
"Empty Menu"
and no options. Scobey::Menu::Menu | ( | const string & | menuTitle | ) |
Constructor for creating a menu with a user-supplied title but no options. @param menuTitle The text of the title for this menu. @pre <em>menuTitle</em> has been initialized and does not exceed 70 characters in length. @post - Case 1 (typical): A menu has been constructed with a title given by the contents of <em>menuTitle</em>, and having 0 options. - Case 2 (error): If an attempt was made to use a title whose length exceeded the maximum length of 70 characters, the following message has been displayed and the user has pressed Enter:
===== Error: Menu title cannot exceed 70 characters in length. The title <<text_of_title_that_was_too_long_appears_here>> was not added to the menu. This menu has been given the following default title: Empty Menu Press Enter to continue ...
void Scobey::Menu::setTitle | ( | const string & | menuTitle | ) |
Sets (or resets) the menu title.
menuTitle | Contains the text of the title for the menu. |
===== Error: Menu title cannot exceed 70 characters in length. The title <<text_of_title_that_was_too_long_appears_here>> was not added to the menu. Previous title remains unchanged. Press Enter to continue ...In either case, the number of menu options is unchanged.
void Scobey::Menu::addOption | ( | const string & | option | ) |
Adds a new option to the menu and assigns it the next available option number. @return void @param option The text of the next option to be added to the menu. @pre The number of options on the menu is < 20.\n <em>option</em> has been initialized and has length <= 70 characters. @post - Case 1 (typical): The number of menu options has been incremented by 1 and the text in <em>option</em> has become the option with the new option number. - Case 2 (error): If the number of options was 20, then the following message has been displayed and the user has pressed Enter:
===== Error: Number of menu options cannot exceed 20. Option <<text_of_option_goes_here>> not added. Press Enter to continue ...
===== Error: Menu option cannot exceed 70 characters in length. The option <<text_of_option_that_was_too_long_appears_here>> was not added to the menu. Press Enter to continue ...
void Scobey::Menu::display | ( | ) | const |
Displays a "centered" menu on the screen. @return void @pre The menu has been initialized. @post The menu has been displayed on the screen, more or less centered left-to-right and top-to-bottom, but with a slight top-left bias. Note, however, that a blank menu, or a menu with just a title, is displayed with its title left-justified and with only the bias toward the top. Such a title is followed by a blank line and the following 1-line message, which also starts at the left margin:
This menu currently has no options ...
The (typical) display format is achieved in the following way:
int Scobey::Menu::getChoice | ( | int | maxNumTries = 3 , |
string | userPrompt = "Enter the number of your menu choice here and press Enter: " |
||
) | const |
Gets a menu choice from the user, or a default value if the user fails to enter a valid menu choice before the permitted number of attempts runs out. @return The user's menu choice, or the default value of -1. @param maxNumTries The maximum number of tries permitted to the user to enter a valid menu choice. @param userPrompt The text of the prompt to be used to ask the user for a menu choice. @pre Typically, a menu has been initialized, and presumably displayed, though in fact this is not technically a necessary pre-condition for this function. @post - Case 1 (typical): A valid menu choice for the menu has been entered by the user and returned. - Case 2 (error): If a choice was sought from a blank menu, the message
===== Error: The menu has no options from which to choose. Press Enter to continue ...
has been displayed and the user has pressed Enter.
===== Error: Sorry! Invalid response. Please try again:If maxNumTries unsuccessful attempts have been made, the displayed message is:
===== Error: Sorry, but you only get <<maxNumTries>> chances. Press Enter to continue ...and the user has pressed Enter, after which the value of -1 is returned. Note that if no prompt is supplied by the client, the default prompt
Enter the number of your menu choice here and press Enter:is displayed slighty left-of-center on the screen below the menu. Note as well that anything other than a valid menu choice for the current menu will be read and rejected, including (for example) a valid menu choice followed by extraneous characters, as well as simply pressing Enter.