Take the 2-minute tour ×

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;
        wcout << L"Please type param #" << argc++ << L":" << endl;
        wcin >> userinput;
        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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

This behavior appears to occur when the "Treat wchar_t as 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_t becomes a typedef for unsigned short, which means you can no longer overload on it. As a result the expression wcin >> userabort invoked the operator>> for unsigned short rather than the one for wchar_t, which doesn't give the correct result.

share|improve this answer
Fixed it, thanks a lot. Do you know why though? –  Felix Dombek Aug 26 '11 at 13:57
@Felix I think I figured it out. It has to do with how overload resolution is done on operator>>. I added the info to the answer. –  Sven Aug 26 '11 at 14:05
add comment

I have no explanation for why this code would work in one program but fail in another. It looks wrong to me. I think you are misunderstanding what wcin.clear(); does. I guess you are thinking that clears any pending input from the stream, but it doesn't. The way to do that is this mouthful of code


You would need to #include <limits> to get std::numeric_limits.

share|improve this answer
I added clear() to look into this error, just for a test. It worked without it as well in one project, and didn't in another. I removed it from my code now to make it more readable as it really makes no difference. Sorry that it puts your remark somewhat out of context. Thanks for the answer though, I will look into the meaning of clear() on input streams. –  Felix Dombek Aug 26 '11 at 13:54
add comment

Your Answer


