-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.cpp
120 lines (107 loc) · 3.69 KB
/
Menu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/****************************************************************
** Program name: Final Project
** Author: Milad Mikeal
** Date: 11/23/18
** Description: Reusable menu class. Class can add menu options,
* display the menu, edit existing menu options, and prompts
* user for input, using the getInt function for validation.
****************************************************************/
#include <iostream>
#include <limits>
#include <stdlib.h>
#include "Menu.hpp"
#include "utilityFunctions.hpp"
using std::cout;
using std::cin;
using std::endl;
using std::string;
/****************************************************************
** Description: Constructor. Initializes numOptions to 0.
****************************************************************/
Menu::Menu() {
heading = "Menu:";
numOptions = 0;
}
Menu::Menu(std::string h) {
heading = h;
numOptions = 0;
}
/****************************************************************
** Description: Displays the menu options.
****************************************************************/
void Menu::display(int type) {
if (type == 0) {
cout << '\n';
cout << heading << endl;
for (int i = 0; i < numOptions; i++) {
cout << (i + 1) << ". " << options.at(i) << '\n';
}
} else {
cout << '\n';
cout << heading << endl;
cout << "w. " << options.at(0) << '\n'
<< "s. " << options.at(1) << '\n'
<< "a. " << options.at(2) << '\n'
<< "d. " << options.at(3) << '\n'
<< "q. " << options.at(4) << '\n';
}
}
/****************************************************************
** Description: This method adds an option to the menu.
****************************************************************/
void Menu::add(string text) {
options.push_back(text);
numOptions++;
}
/****************************************************************
** Description: This method edits the label of an existing
* option.
****************************************************************/
void Menu::edit(int num, string text) {
options.at(num-1) = text;
}
/****************************************************************
** Description: This method deletes an option from the menu.
****************************************************************/
void Menu::del(int num) {
options.erase(options.begin() + (num - 1));
numOptions--;
}
/****************************************************************
** Description: This method displays the menu, prompts the user
* for input, and returns the input.
****************************************************************/
int Menu::prompt() {
display(0);
return input();
}
/****************************************************************
** Description: This method returns the number of options.
****************************************************************/
int Menu::getNum() {
return numOptions;
}
/****************************************************************
** Description: This method returns the label of an option.
****************************************************************/
string Menu::getOption(int num) {
if (num < 1 || num > numOptions) {
return "ERROR. No menu option.";
}
return options.at(num - 1);
}
/****************************************************************
** Description: This method returns the user's selection if it
* is valid.
****************************************************************/
int Menu::input() {
if (numOptions > 0) {
cout << '\n';
int selection;
getInt(&selection, 1, numOptions);
return selection;
} else {
cout << "ERROR. No menu options available.\n";
return -1;
}
}