-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
84 lines (68 loc) · 2.06 KB
/
Copy pathMenu.cpp
File metadata and controls
84 lines (68 loc) · 2.06 KB
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
/*********************************************************************
** Program name: FinalProject
** Author: James Scanlon
** Date: March 19, 2019
** Description: Implementation of the Menu class.
*********************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include "Menu.hpp"
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
Menu::Menu(vector<string> choices)
{
setMenuChoices(choices);
// Set prompt text to null if it doesn't exist
setPromptText("");
}
Menu::Menu(string text, vector<string> choices)
{
setMenuChoices(choices);
setPromptText(text);
}
void Menu::setMenuChoices(vector<string> choices)
{
// Clear the menu before creating a new array
menuChoices.clear();
for (string i : choices) {
menuChoices.push_back(i); // Flip has trouble with just taking a vector string as an arg, so I'm using .push_back
}
}
void Menu::setPromptText(string text)
{
promptText = text;
}
string Menu::getPromptText()
{
return promptText;
}
int Menu::showMenu()
{
// Create a variable to hold the user's menu choice
int choice = 0;
// Show the prompt, if there is one
if (getPromptText() != "") {
cout << "\n" << getPromptText() << "\n";
}
// Show the user the menu
for (int i = 0; i < menuChoices.size(); i++) {
// Show the menu item
cout << i + 1 << ". " << menuChoices[i] << "\n";
}
// Get input from the user
cin >> choice;
while (cin.fail()|| (choice < 1 || choice > menuChoices.size())) // ensure that user choice is within range
{
cin.clear();
cin.ignore(4000,'\n'); // prevent strings from outputting multiple errors
cout << "Invalid choice; please input a valid number between 1 and " << menuChoices.size() << "." << endl;
cin >> choice; // take in another input
}
return choice;
}
Menu::~Menu(){
}