-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.cpp
422 lines (376 loc) · 13.7 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/******************************************************************************
** Menu Class Implementation File
** Author: Lauren "Ren" Demeis-Ortiz
** Date: 10/8/19 Updated: 11/29/19
** Description: This file contains the implementation of the Menu class. It
** has functions that display a menu, get a user inputted choice,
** validate the choice, and return the choice client program.
******************************************************************************/
#include "Menu.hpp"
#include <iostream>
#include <iomanip>
#include <limits>
#include <sstream>
/*****************************************************************************
** Menu()
** Description: Default constructor sets displayMenu to "No menu available"
** and number of choices and choice are set to 0.
*****************************************************************************/
Menu::Menu()
{
numChoices = 1;
choice = 0;
displayMenu = "No Menu Available. Enter 1 to exit.";
type = 1;
}
/*****************************************************************************
** Menu(std::string, int)
** Description: Constructor that takes a string and an integer parameter:
** the menu to be displayed on the console and the number of
** choices available for the menu. Choice is set to 0.
**
*****************************************************************************/
Menu::Menu(std::string menu, int numChoices)
{
this -> numChoices = numChoices;
displayMenu = menu;
choice = 0;
type = 1;
}
/*****************************************************************************
** Menu(std::string, std::string, int, int)
** Description: Constructor that takes two string and two integer parameters:
** the menu to be displayed on the console, the input category
** and the lower limit menu input and upper limit of menu input.
** Choice is set to 0.
**
*****************************************************************************/
Menu::Menu
(std::string menu, std::string inputCategory, int lowerLimit, int numChoices)
{
this -> numChoices = numChoices;
this -> inputCategory = inputCategory;
this -> lowerLimit = lowerLimit;
displayMenu = menu;
choice = 0;
type = 2;
}
/*****************************************************************************
** printMenu()
** Description: Displays menu on console.
*****************************************************************************/
void Menu::printMenu()
{
std::cout << displayMenu << std::endl;
}
/*****************************************************************************
** setNumChoices(int)
** Description: Sets number of choices from an integer parameter.
*****************************************************************************/
void Menu::setNumChoices(int num)
{
numChoices = num;
}
/*****************************************************************************
** setChoice()
** Description: Sets and validates choice from user input.
*****************************************************************************/
void Menu::setChoice()
{
std::string strIntChoice; // Stores users choice as string for validation
std::getline(std::cin, strIntChoice); // Get choice from user input
if (type == 2)
{
// Validate choice is an integer.
choice = this ->
isValid(strIntChoice, inputCategory, lowerLimit, numChoices);
}
else
{
// Validate choice is an integer.
choice = this ->
isValid(strIntChoice, "menu choice number", 1, numChoices);
}
}
/*****************************************************************************
** setChoice(int)
** Description: Sets choice to integer parameter.
*****************************************************************************/
void Menu::setChoice(int choice)
{
this-> choice = choice;
}
/*****************************************************************************
** setStrChoice()
** Description: Sets and validates to and from string user input including
** spaces.
** Credit: https://www.geeksforgeeks.org/
** how-to-use-getline-in-c-when-there-are-black-lines-in-input/
*****************************************************************************/
void Menu::setStrChoice()
{
getline(std::cin, strChoice);
while (strChoice.length() == 0)
{
std::cout <<
"Oops you hit enter without typing anything. Please enter something."
<< std::endl;
getline(std::cin, strChoice);
}
}
/*****************************************************************************
** setDisplayMenu(std::string)
** description: Sets menu text to be displayed.
*****************************************************************************/
void Menu::setDisplayMenu(std::string menu)
{
displayMenu = menu;
}
/*****************************************************************************
** setType(int)
** description: Sets type.
*****************************************************************************/
void Menu::setType(int type)
{
this-> type = type;
}
/*****************************************************************************
** getchoice()
** description: returns choice.
*****************************************************************************/
int Menu::getChoice()
{
return choice;
}
/*****************************************************************************
** getStrChoice()
** description: returns strChoice.
*****************************************************************************/
std::string Menu::getStrChoice()
{
return strChoice;
}
/*****************************************************************************
** runMenu()
** description: runs menu.
*****************************************************************************/
void Menu::runMenu()
{
this->printMenu();
this->setChoice();
}
/******************************************************************************
** isValid(string, string)
** Description: This function validates parameter is an integer. It
** takes two string parameters: the user's input and input
** category user is inputting. For example "choice" or
** "row". It does not test a valid range.
******************************************************************************/
int Menu::isValid(std::string userInput, std::string inputCat)
{
std::string strInput = userInput;
bool testInt = 1;
if (strInput.length() < 1)
{
strInput = "\n";
}
// Validate that user input did not cause input error
// Credit: Doug T. https://stackoverflow.com/questions/5864540/
// infinite-loop-with-cin-when-typing-string-while-a-number-is-expected
while (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Prompt user and get new input
std::cout << "Oops! Something went wrong. Try again.\n"
"Please enter " << inputCat << "."<< std::endl;
std::getline(std::cin, strInput);
if (strInput.length() < 1)
{
strInput = "\n";
}
}
// Check that number is an integer
do
{
if(strInput[0] == '-' && strInput.length() > 1) // If negative ignore -
{
// Reset test for next input
testInt = 1;
for (unsigned i = 1; i < strInput.length(); ++i)
{
if (!isdigit(strInput[i]))
{
testInt = 0;
}
}
if (testInt == 0)
{
// Prompt user to enter an integer
std::cout << "Oops! Try again. You must enter an integer.\n"
"Please enter " << inputCat << "."<< std::endl;
// Get new input from user
std::getline(std::cin, strInput);
if (strInput.length() < 1)
{
strInput = "\n";
}
}
}
else
{
// Reset test for next input
testInt = 1;
for (unsigned i = 0; i < strInput.length(); ++i)
{
if (!isdigit(strInput[i]))
{
testInt = 0;
}
}
if (testInt == 0)
{
// Prompt user to enter an integer
std::cout << "Oops! Try again. You must enter an integer.\n"
"Please enter " << inputCat << "."<< std::endl;
// Get new input from user
std::getline(std::cin, strInput);
if (strInput.length() < 1)
{
strInput = "\n";
}
}
}
} while (testInt == 0);
// Convert string user input to an integer
// Credit: https://www.geeksforgeeks.org/converting-strings-numbers-cc/
std::stringstream inputStream(strInput);
int input;
inputStream >> input;
return input;
}
/******************************************************************************
** isValid(string, string, int, int)
** Description: This function validates user input from. It
** takes two string parameters: the user's input and input
** category user is inputting. For example "choice" or
** "row" and two integer parameters: lower limit of
** valid range and upper limit of valid range.
******************************************************************************/
int Menu::isValid
(std::string userInput, std::string inputCat, int lowLim, int upLim)
{
std::string strInput = userInput;
bool testInt = 1;
if (strInput.length() < 1)
{
strInput = "\n";
}
// Validate that user input did not cause input error
// Credit: Doug T. https://stackoverflow.com/questions/5864540/
// infinite-loop-with-cin-when-typing-string-while-a-number-is-expected
while (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Prompt user and get new input
std::cout << "Oops! Something went wrong. Try again.\n"
"Please enter " << inputCat << "."<< std::endl;
std::getline(std::cin, strInput);
if (strInput.length() < 1)
{
strInput = "\n";
}
}
// Check that number is an integer
do
{
// Reset test for next input
testInt = 1;
for (unsigned i = 0; i < strInput.length(); ++i)
{
if (!isdigit(strInput[i]))
{
testInt = 0;
}
}
if (testInt == 0)
{
// Prompt user to enter an integer
std::cout << "Oops! Try again. You must enter an integer.\n"
"Please enter " << inputCat << "."<< std::endl;
// Get new input from user
std::getline(std::cin, strInput);
if (strInput.length() < 1)
{
strInput = "\n";
}
}
} while (testInt == 0);
// Convert string user input to an integer
// Credit: https://www.geeksforgeeks.org/converting-strings-numbers-cc/
std::stringstream inputStream(strInput);
int input;
inputStream >> input;
// Validate input is in proper range
while (input < lowLim || input > upLim)
{
// Prompt user and get new input
std::cout <<
"Oops! Try again. You must enter a number between " << lowLim <<
" and " << upLim << ". \n"
"Please enter the number of " << inputCat << "."
<< std::endl;
std::getline(std::cin, strInput);
if (strInput.length() < 1)
{
strInput = "\n";
}
// Validate that user input did not cause input error
// Credit: Doug T. https://stackoverflow.com/questions/5864540/
// infinite-loop-with-cin-when-typing-string-while-a-number-is-expected
while (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Prompt user and get new input
std::cout << "Oops! Something went wrong. Try again.\n"
"Please enter " << inputCat << "."<< std::endl;
std::getline(std::cin, strInput);
if (strInput.length() < 1)
{
strInput = "\n";
}
}
// Check that number is an integer
do
{
// Reset test for next input
testInt = 1;
for (unsigned i = 0; i < strInput.length(); ++i)
{
if (!isdigit(strInput[i]))
{
testInt = 0;
}
}
if (testInt == 0)
{
// Prompt user to enter an integer
std::cout << "Oops! Try again. You must enter an integer.\n"
"Please enter " << inputCat << "."<< std::endl;
// Get new input from user
std::getline(std::cin, strInput);
if (strInput.length() < 1)
{
strInput = "\n";
}
}
} while (testInt == 0);
// Convert string user input to an integer
// Credit: https://www.geeksforgeeks.org/converting-strings-numbers-cc/
std::stringstream inputStream(strInput);
inputStream >> input;
}
return input;
}