stringstream string to int
<p>The C++ below does an int to string, int to string and a string to int.  It then repeats these steps again.  This stringstream to int line [stream1 >> i3;]  is breaking the code.  What am I missing here?</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
using namespace std;

int main()
{
 int i1 = 101;
 int i2 = 202;
 int i3 = 0;
 string s1 = "";
 string s2 = "";
 stringstream stream1;

 for (int i=0;i&lt;2;i++)
 {

   //int to string
   stream1.str("");
   cout &lt;&lt; "i1 " &lt;&lt; i1 &lt;&lt; endl;
   stream1 &lt;&lt; i1;
   s1 = stream1.str();
   cout &lt;&lt; "s1 " &lt;&lt; s1 &lt;&lt; endl;

   //int to string
   cout &lt;&lt; "i2 " &lt;&lt; i2 &lt;&lt; endl;
   stream1.str("");
   stream1 &lt;&lt; i2;
   s2 = stream1.str();
   cout &lt;&lt; "s2 " &lt;&lt; s2 &lt;&lt; endl;

   //string to int
   stream1.str("");
   stream1.str(s2);
   stream1 &gt;&gt; i3;
   //line above causes s1 and s2 to get messed up during 2nd time in for loop
   cout &lt;&lt; "i3-- " &lt;&lt; i3 &lt;&lt; endl;

  }
  return 0;
 }
</code></pre>
