-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_util.cpp
More file actions
59 lines (52 loc) · 1.63 KB
/
Copy pathinput_util.cpp
File metadata and controls
59 lines (52 loc) · 1.63 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
#include "input_util.h"
int input_util::getIntInput() {
string in = getStringInput(std::regex("^\\d+$"),
"Given input is not a number");
int ret = 0;
auto stream = std::stringstream(in);
stream >> ret;
return ret;
}
int input_util::getOptionUserInput(int max) {
int input = -1;
bool valid = false;
while (!valid) {
input = getIntInput();
if (input < 1 || input > max) {
std::cout << "Input must be between 1 and " << max << std::endl;
} else {
valid = true;
}
}
return input;
}
string input_util::getStringInput(const std::regex ®ex,
const string &error) {
string input;
bool valid = false;
while (!valid) {
std::cout << "> " << std::flush;
input_util::getline(std::cin, input);
if (!std::cin.eof()) {
// check if input matches given regex
bool found = std::regex_search(input, regex);
if (!found) {
std::cout << error << std::endl;
} else {
valid = true;
}
} else {
// if input read is eof, terminate
QwirkleGameEngine::quit();
}
}
return input;
}
void input_util::getline(std::basic_istream<char> &stream, string &input) {
std::getline(stream, input);
// if on windows,
// lines might have CR (\r) at the end, so remove if its there...
if (!input.empty() && input[input.size() - 1] == '\r') {
input.erase(input.size() - 1);
}
}