-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu.cpp
More file actions
44 lines (37 loc) · 1.08 KB
/
Copy pathmenu.cpp
File metadata and controls
44 lines (37 loc) · 1.08 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
#include <iostream>
#include <cstdlib>
#include "menu.h"
void Menu::AddOption(std::string text, std::function<void()> callback) {
std::unique_ptr<Option> option(new Option);
option->text = std::move(text);
option->callback = std::move(callback);
display_options_.push_back(option.get());
options_.push_back(std::move(option));
}
Menu::Result Menu::Execute() {
int i = 1;
for (const auto* option : display_options_) {
if (option == nullptr) {
std::cout << std::endl;
} else {
std::cout << i << ": " << option->text << std::endl;
++i;
}
}
std::cout << "Enter a number OR enter q/Q." << std::endl << std::endl;
std::string ans;
std::cin >> ans;
if (ans == "q" || ans == "Q") {
return Result::kQuit;
}
int selection = strtol(ans.c_str(), nullptr, 10);
if (selection <= 0 || selection > options_.size()) {
std::cout << "Sorry! Didn't understand that!" << std::endl;
return Result::kNoAction;
}
options_[selection - 1]->callback();
return Result::kAction;
}
void Menu::BlankLine() {
display_options_.emplace_back(nullptr);
}