I have a function which asks for a number and keeps reasking until a real number is given, and then returns that number. It works perfectly with cin, but it doesn't when I convert everything to the wide version.
Working version:
Not working version: cin becomes wcin, cout becomes wcout and string becomes wstring.Code:int GetCinIntInput(const string& AskText, const string& ErrorText) const
{
bool ErrorDisplay = false;
int Input;
do
{
if (ErrorDisplay) cout << 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
cin.clear();//reset errorflags
cin.ignore(cin.rdbuf()->in_avail()); //ignore everything that's in the buffer
cout << AskText << ": "; //ask the user for the input (textwise)
}while(!(cin >> Input)); //await input and look if it goes into the integer, if it does, the loop exits and mission accomplished
return Input;
}
It compiles but it just turns into an infinite loop of the error msg (if input is a text, it works if the input is a number).