The code below shows the output as
enter something abc you entered: a you entered: b you entered: c
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char c;
while(cin>>c)
{
//Do something
cout<<"you entered: "<<c<<"\n";
}
return 0;
}
why is it not showing only the first character entered? I know I can force it to ignore the cin buffer after first char by using
cin.ignore(1,'\n')
but shouldnt it only ready one character and ignore the rest?
No. It works correctly and I do not know where you got the idea that it should ignore something. It reads whatever is there in the buffer, and blocks waiting for more input iff the buffer is empty. In your case it gets empty only after three iterations of the while loop.