+ 4
It will work as you expect, if you cleared the underlying string buffer of <ss2> at line 12 ss2.str( string {} ); clear() method apparently was meant to clear the state flags e.g. error flags etc. It wasn't meant to clear the underlying string buffer. Lisa's way is simpler I think ... https://en.cppreference.com/w/cpp/io/basic_ios/clear
2nd Jul 2022, 9:36 AM
Ipang
+ 3
Manav Roy clear your ss2 and ss ss2.clear(); ss2.str(string{}); see my comment on your Hello Bro example.
3rd Jul 2022, 1:17 AM
Bob_Li
Bob_Li - avatar
+ 2
// I think ss2 needs to be a string and go into the head of the while-loop #include <iostream> #include <sstream> using namespace std; int main() { string s="hello how are you"; stringstream ss(s); string ss2; while(ss>>ss2) { cout << ss2 << endl; } return 0; } // https://www.tutorialspoint.com/stringstream-in-cplusplus
2nd Jul 2022, 9:11 AM
Lisa
Lisa - avatar
+ 2
I couldn't figure out what was wrong with your code, it goes crashing with a message I couldn't understand. https://code.sololearn.com/c51M6TgksDru/?ref=app
2nd Jul 2022, 1:48 PM
Ipang
+ 1
ss is the stringstream
2nd Jul 2022, 9:32 AM
Lisa
Lisa - avatar
+ 1
No problem : )
2nd Jul 2022, 12:42 PM
Ipang