-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-validation.hpp
More file actions
66 lines (49 loc) · 1.18 KB
/
input-validation.hpp
File metadata and controls
66 lines (49 loc) · 1.18 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
/** Input Validation Module
* Author: Jose Lopez
*
* Description:
* Header-only runtime input validation
*
* Input Validation:
*
* The caller specifies the required input
* type in the function call as follows:
*
* auto userResponse = IO::get<[TYPE]>();
*
* Example:
* auto age = IO::get<int>();
* auto name = IO::get<std::string>();
*
*
* Contributors:
* Daniel Jour (https://codereview.stackexchange.com/users/20977/daniel-jour)
* Loki Astari (https://codereview.stackexchange.com/users/507/loki-astari)
*/
#ifndef _INPUT_VALIDATION_HPP
#define _INPUT_VALIDATION_HPP
#include <iostream>
#include <limits>
#include <typeinfo>
// TODO: Fix module "half-checks"
namespace IO {
template <typename T>
T get()
{
auto IO_status_flag = false;
T response;
std::cin >> response;
do {
if (std::cin.fail()) {
std::cout << "[Error] Please enter a valid response: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin >> response;
} else {
IO_status_flag = true;
}
} while (IO_status_flag != true);
return response;
}
}
#endif /* _INPUT_VALIDATION_HPP */