/
menu.cpp
66 lines (55 loc) · 1.62 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
/*********************************************************************
** Program name: menu.cpp
** Author: Jesse McKenna
** Date: 10/3/2017
** Description: Implementation file for menu functions.
*********************************************************************/
#include "menu.hpp"
#include "getInput.hpp"
#include <iostream>
// --- Menu ---
// Default constructor, called with the desired menu size; menu size cannot be
// greater than MAX_PROMPTS or it will be limited to MAX_PROMPTS
Menu::Menu(int menuSize)
{
size = menuSize;
if (size > MAX_PROMPTS)
{
size = MAX_PROMPTS; // restrict size to MAX_PROMPTS limit
}
prompts = new std::string[size];
}
// --- ~Menu ---
// Destructor; deletes the dynamically allocated string array "prompts"
Menu::~Menu()
{
delete [] prompts;
prompts = nullptr;
}
// --- setPrompt ---
// Accepts a prompt position and a string as arguments; puts the string into the
// "prompts" array at the specified position
void Menu::setPrompt(int number, std::string prompt) // set the desired prompt text
{
if (number <= 0 || number > size)
{
return; // do nothing if invalid number referenced
}
prompts[number - 1] = prompt; // put entered prompt into the "prompts" array
}
// --- showMenu ---
// Displays the menu by printing each prompt in "prompts", then returns the
// selection made by the user
int Menu::showMenu()
{
std::cout << "\n";
for (int i = 0; i < size; i++)
{
std::cout << i + 1 << ". "; // prompt numbering, starting at 1
std::cout << prompts[i]; // prompt text
std::cout << "\n\n";
}
int choice;
choice = getInt(1, size);
return choice;
}