I have this code for example:
#include <iostream>
#include <sstream>
using namespace std;
int main(){
stringstream ss;
string buffer,first_word;
int i;
for(i = 0; i < 4; i++){
getline(cin,buffer); // getting line
ss.str(buffer); // initializing the stream
ss>>first_word;
cout<<first_word<<endl;
ss.str(string()); // cleaning stream
}
return 0;
}
with this input:
line one with spaces
line two with spaces
alone
line four with spaces
The output I expect is just the first words of the lines, like this:
line
line
alone
line
But I am getting this:
line
line
alone
alone
So, stringstream
is not updating after getting a line that only has one word.
Please explain this to me, I don't want the right answer with code, I want to know why.
Thank you.