I am little confused about the stringstream. Here are the two programs which are behaving very differently
int main(){
stringstream s("1,-5,-10,20,");
int i;
char ch;
while(!s.eof()){
s >> i;
s >> ch;
cout<<i<<" " << ch << " " << s.eof() <<endl;
}
stringstream s2(string("the cattle was rattled by the battery"));
string mys;
while(!s2.eof()){
s2 >> mys;
cout<<mys <<" " << s2.eof() <<endl;
}
return 0;
}
The first stream does not stop after the extraction of the last token "," while for the second stringstream s2 it stopped after the extraction of last token battery. Why is the s.eof() behaviour different in these two scenerio?
The answer in the dupliacate questions does not correctly tell why the second while loop is working perfectly below. (for string s2). This is not a duplicate