-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.hpp
More file actions
29 lines (27 loc) · 941 Bytes
/
util.hpp
File metadata and controls
29 lines (27 loc) · 941 Bytes
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
#include "util.h"
/**
* This specification needs to be here, since templates are only compiled when 'compilation units' use them.
* To avoid problems, a .hpp is used which is treated as a compilation unit.
* This prevents confusion.
* @tparam T the type of the value being read.
* @return the value that was read from the std::in stream.
*/
template<class T>
T ReadAndValidate()
{
T n;
std::cout << ">";
std::cout.flush();
std::cin >> n;
while (std::cin.fail() == 1)
{
std::cin.clear();
std::cin.ignore(1000, '\n'); //throw away 1000 chars, or next end of line
std::cout << "\nincorrect value, please enter correct value\n>";
std::cin >> n;
}
//Get rid of unconsumed enter that follows values to avoid 'autopress' in press any key to continue.
// an enter is always present, since it is used to terminate the input.
std::cin.ignore(1000, '\n');
return n;
}