Summary Files Commits Branches @commit @diff @stats
path: root / src / cli.hpp
blob: b7d4c3013e326698c73561260b8d9779e6b63bca (plain) (blame)
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
#ifndef GS_CLI
#define GS_CLI

#include <iostream>
#include <limits>
#include <stdio.h>

namespace cli {
	template <typename T>
	void readUserInput(T& outValue, const char* enter_message, const char* error_message) {
		std::cout << enter_message;
		std::cin >> outValue;

		while (std::cin.fail()) {
			std::cerr << error_message << std::endl;

			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

			std::cout << enter_message;
			std::cin >> outValue;
		}
	}

	template <typename T>
	void readUserInputMax(T& outValue, T max_value, const char* enter_message, const char* error_message) {
		readUserInput(outValue, enter_message, error_message);
		while (outValue >= max_value) {
			std::cerr << error_message << std::endl;
			readUserInput(outValue, enter_message, error_message);
		}
	}

	template <typename T, typename U>
	void scanTwoValues(const char* format, T& outFirst, U& outSecond, const char* enter_message, const char* error_message) {
		std::cout << enter_message;
		while (scanf(format, &outFirst, &outSecond) != 2) {
			std::cerr << error_message << std::endl;
			std::cout << enter_message;

			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
	}
}

#endif /* GS_CLI */