Originally asked here.
If I send “Hello world” to cin with the following code…
string str,msg;
getline(cin,str);
stringstream ss(str);
char c;
while(ss >> noskipws >> c)
msg += c;
…results in msg = "Hello World".
But if I use string c instead of char c, msg is empty.
I am compiling with:
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Why do I get an empty string?
Here’s an interesting quote from cplusplus.com:
Remove the
noskipwsif you want to extract tokens into a string.Generally, you should consider carefully, and then stick to one of, { line-based, token-based, or character-based } extraction.