-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cpp
62 lines (57 loc) · 1.76 KB
/
Utils.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
#include "Utils.h"
#include <cctype>
#include <iostream>
#include <limits>
void cinIgnore()
{
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
int readBoundedInt(int min, int max)
{
for (;;) {
int input;
std::cin >> input;
if (std::cin.fail()) {
std::cout << "Error: This is not an integer number.\n";
std::cin.clear();
} else if (std::cin.peek() != '\n') {
std::cout << "Error: Unexpected characters after the number.\n";
} else if (input < min || input > max) {
std::cout << "Error: The number should be betweeen "
<< min << " and " << max << ".\n";
} else { // no errors
std::cin.get(); // remove line break from buffer
return input;
}
// ignore rest of the input if there is any
cinIgnore();
}
}
char readBoundedChar(int min, int max, bool ignoreCase)
{
for (;;) {
char input;
std::cin >> input;
if (std::cin.peek() != '\n') {
std::cout << "Error: More than 1 character was given.\n";
} else if ((input < min || input > max)
&& (!ignoreCase || tolower(input) < tolower(min)
|| tolower(input) > tolower(max))) {
std::cout << "Error: The character should be betweeen "
<< char(min) << " and " << char(max) << ".\n";
} else { // no errors
std::cin.get(); // remove line break from buffer
return input;
}
// ignore rest of the input if there is any
cinIgnore();
}
}
bool containsChar(const char* arr, int len, char c)
{
for (int i = 0; i < len; ++i) {
if (arr[i] == c)
return true;
}
return false;
}