1

Stringstream is very convenient in C++ to convert between strings and other num types. I am trying to use a stringstream multiple times in a program like this.

stringstream ss;
string s1="stack",s2="overflow";
string s3,s4;
ss<<s1;
ss>>s3;
ss.str("");  
cout<<s3<<endl;

ss<<s2;
ss>>s4;
cout<<s4<<endl;

The program will correctly output "stack", but will not output "overflow". Any explanations for that?

richard.g
  • 3,585
  • 4
  • 16
  • 26

1 Answers1

0

call ss.clear()

stringstream ss;
string s1="stack",s2="overflow";
string s3,s4;
ss<<s1;
ss>>s3;
ss.str("");  
cout<<s3<<endl;
ss.clear()
ss<<s2;
ss>>s4;
cout<<s4<<endl;
Mukt G
  • 101
  • 3