copy paste:
Code:
bool ErrorDisplay = false;
int Input;
do
{
if (ErrorDisplay) wcout << ErrorText << endl; //if error should be displayed display it
ErrorDisplay = true; //first time in the loop it's not an error, but from this point it will always be an error until the loop ends
wcin.clear();//reset errorflags
wcin.ignore(wcin.rdbuf()->in_avail()); //ignore everything that's in the buffer
wcout << AskText << L": "; //ask the user for the input (textwise)
}while(!(wcin >> Input)); //await input and look if it goes into the integer, if it does, the loop exits and mission accomplished
return Input;
it seems
Code:
wcin.rdbuf()->in_avail()
always returns 0, while it should be returning the number of characters in the buffer that are ready to be read.
I have constructed something else in the mean time, which does the same but works:
Code:
int ServerEngine::GetCinIntInput(const tstring& AskText, const tstring& ErrorText) const
{
bool ErrorDisplay = false;
int Input;
tstringstream tmp;
do
{
if (ErrorDisplay) tcout << ErrorText << endl; //if error should be displayed display it
ErrorDisplay = true; //first time in the loop it's not an error, but from this point it will always be an error until the loop ends
tcout << AskText << _T(": "); //ask the user for the input (textwise)
TCHAR buf[1024];
tcin.getline(buf,1024);
tmp.clear();
tmp.str(_T(""));
tmp << buf;
}while(!(tmp >> Input)); //await input and look if it goes into the integer, if it does, the loop exits and mission accomplished
return Input;
}
This is using some defines to switch between mbcs and unicode based on project settings.
However if somebody finds out why that other method didn't work, I'm always happy to hear it and learn something new.