/
menu.cpp
72 lines (65 loc) · 1.93 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
/***************************************************************************************************
** Author: Kurt Kaiser
** Website: kurtkaiser.us
** Info: Designed a one-player, text-based game, the player can move through
** spaces to get items and accomplish goals.
****************************************************************************************************/
#include <iostream>
#include <string>
#include <limits.h>
#include "menu.hpp"
using std::cout;
using std::string;
using std::endl;
using std::cin;
// Constructor for the Menu class
Menu::Menu(string option1) {
// Declaring private member variables and arrays of variables
length = 1;
options = new string[length];
options[0] = option1;
start = 1;
}
// Defining methods to access and set class variables
void Menu::addOption(std::string optionIn) {
string *tempOptions = new string[length + 1];
for (int i = 0; i < length; i++){
tempOptions[i] = options[i];
}
tempOptions[length] = optionIn;
delete [] options;
options = tempOptions;
length++;
}
// Setter for start value, used for choice validation
void Menu::setStart(int startIn) {
start = startIn;
}
void Menu::editOption(int index, string change) {
options[index] = change;
}
// Output the options with numbers for selection
// Prompt user for input, validate input and return it
int Menu::getUserChoice() {
int choice = 0;
for (int i = 0; i < length; i++){
cout << (i + 1) << ": ";
cout << options[i] << endl;
}
while (true) {
// if input is an int and in the acceptable range break
if (cin >> choice) {
if (choice > (start - 1) && choice < (length + 1)) {
break;
}
}
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Please enter a number from " << start << " to " << length << "." << endl;
}
// Return acceptable user input
return choice;
}
void Menu::deleteMenu() {
delete[] options;
}