Huh...
Well, it's a shame I went ahead without it then. I finished it:
Code:
#include <boost/format.hpp>
#include <iostream>
#include <string>
#include <sstream>
namespace Input
{
template<typename StrT> void StreamOut(const StrT& Output) { std::wcout << Output; }
void StreamOut(const char* Output) { std::cout << Output; }
void StreamOut(const std::string& Output) { std::cout << Output; }
template<typename T> void StreamOut(const boost::basic_format<T>& Output) { StreamOut(Output.str()); }
template<typename StrT, typename T> bool StreamIn(StrT& Stream, T& Buffer)
{
std::wstring Answer;
std::getline(Stream, Answer);
std::wstringstream StrStream(Answer);
return (StrStream >> Buffer) != nullptr;
}
template<typename T> bool StreamIn(T& Buffer) { return StreamIn(std::wcin, Buffer); }
template<typename DataT, typename StrT1, typename StrT2, typename StrT3, typename Validator>
void Read(DataT& Out, const StrT1& Question, const StrT2& ErrorType, const StrT3& ErrorValidation, Validator ValidatorFnc)
{
for (;;)
{
StreamOut(Question);
if (!StreamIn(Out))
{
StreamOut(ErrorType);
continue;
}
if (ValidatorFnc(Out))
break;
StreamOut(ErrorValidation);
}
}
}
int main()
{
int x;
const int Max = 20;
const int Min = 10;
Input::Read
(
x,
L"Enter an integer: ",
L"That is not a valid integer. Try again.\n",
boost::wformat(L"Invalid input. The integer must be bigger than %d and lower than %d.\n") % Min % Max,
[Min, Max](int Value) { return Value >= Min && Value <= Max; }
);
std::cout << "You entered: " << x << std::endl;
}
I must say: this is beautiful. Wonderful. Excellent. Super.