42 lines
1.1 KiB
C++
Executable File
42 lines
1.1 KiB
C++
Executable File
#pragma once
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <ostream>
|
|
#include <string>
|
|
#include <functional>
|
|
|
|
|
|
constexpr auto all = std::numeric_limits<std::streamsize>::max();
|
|
|
|
namespace math
|
|
{
|
|
template<auto lb, typeof(lb) ub, std::enable_if_t<std::is_integral<typeof(lb)>::value, bool> = true>
|
|
constexpr auto is_between(typeof(lb) x) -> bool { return (ub > x) && (x > lb); }
|
|
}
|
|
|
|
template<typename T>
|
|
T get_input(const std::string& prompt, const std::string& on_error, std::function<bool(T)> validator) noexcept
|
|
{
|
|
T user_input{};
|
|
try
|
|
{
|
|
while (true)
|
|
{
|
|
printf("%s", prompt.c_str());
|
|
std::cin >> user_input;
|
|
if (std::cin.fail() || !validator(user_input))
|
|
{
|
|
std::cout << on_error << std::endl;
|
|
std::cin.clear();
|
|
std::cin.ignore(all, '\n');
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
return user_input;
|
|
}
|
|
catch (std::exception&) { return user_input; } /* This should not happen ..... cin/cout are not configured to throw...*/
|
|
}
|
|
|
|
template<typename T>
|
|
T get_input(const std::string& prompt) { return get_input<T>(prompt, "", [](auto) {return true; }); } |