I use this code in MSVC++2010 to implement a user input loop:
int wmain(int argc, wchar_t* argv[])
{
vector<wstring> arguments;
wstring userinput;
wchar_t userabort;
do
{
wcout << L"Please type param #" << argc++ << L":" << endl;
wcin >> userinput;
arguments.push_back(userinput);
userinput.clear();
wcout << L"Type 'y' to input next param, 'n' to start program:" << endl;
wcin >> userabort;
wcout << L"Userabort: " << userabort << " (" << int(userabort) << ")" << endl;
}
while (userabort == L'y' || userabort == L'Y'
|| userabort == L'j' || userabort == L'J');
return 1;
}
In one of my projects, this works flawlessly (in both Debug and Release configuration). In another of my projects, this very same code (also as only code in wmain) behaves strangely (also in both configurations): it does not stop to wait for my input after the “Type ‘y’ to input next param”, but instead immediately reads a linefeed char from the stream.
I even tried wcin >> skipws >> userabort; but same result. I don’t know what could cause that at all. Could it be the result of some compiler setting or imported file? Or is it maybe the result of pressing return to send the userinput wstring to the program? Any help appreciated.
This behavior appears to occur when the “Treat
wchar_tas a built-in type” (/Zc:wchar_t) option is not specified.Go to the project’s properties, under Configuration Properties, C/C++, Language, and make sure that “Treat WChar_t As Built in Type” is set to yes.
This is because when the option is set to no,
wchar_tbecomes atypedefforunsigned short, which means you can no longer overload on it. As a result the expressionwcin >> userabortinvoked theoperator>>forunsigned shortrather than the one forwchar_t, which doesn’t give the correct result.