title: istream for a struct with a vector member
body: <p>Its not very clear to me how istream works  from standard input (i.g. cin>> from keyboard)
   for a structure that has a vector member.
  I have a simple struct with double, string and vector members. I want to read structs from cin, and printing them with cout. I overload the &lt;&lt; and >> operators and here is my code:</p>
  
  <pre><code>    #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;
  
  
  struct Test {
      double d;
      string s;
      vector&lt;int&gt;vi;
      Test():d(0.0),s(string()),vi(0)     
      {}
      Test(double d1,string s1,vector&lt;int&gt;vi1):d(d1),s(s1),vi(vi1)    
      {}
  };
  
  istream &amp;operator&gt;&gt;(istream &amp;is, vector&lt;int&gt;&amp;v)
  {
      int x;
      cout&lt;&lt;"type the vector&lt;int&gt;elements :"&lt;&lt;endl;
      while (is&gt;&gt;x)
          v.push_back(x);
      is.clear();
      return is;
  }
  
  ostream &amp;operator&lt;&lt;(ostream &amp;os, vector&lt;int&gt;&amp;v)
  {
      os&lt;&lt;"[ ";
      for (int i=0;i&lt;v.size();i++)
          os&lt;&lt;v[i]&lt;&lt;" ";
      os&lt;&lt;" ]";
      return os;
  }
  
  istream &amp;operator&gt;&gt;(istream &amp;is, Test &amp;t)
  {
      cout&lt;&lt;"type the double d value: ";
      is&gt;&gt;t.d;
      cout&lt;&lt;"type the string s value: ";
      is.ignore();                    //call ignore before getline
      getline(is,t.s);
      //int x;
      //cout&lt;&lt;"type the vector elements:"&lt;&lt;endl;  //try to use the vector&lt;int&gt; istream operator
      //while (true) {
      //  if (is.eof()==1) break;
      //  t.vi.push_back(x);
      //}
      //is.clear();
      is&gt;&gt;t.vi;
      is.clear();
      return is;
  } 
  
  
  ostream &amp;operator&lt;&lt;(ostream &amp;os, Test &amp;t) 
  {
      os&lt;&lt;"{ ";
      os&lt;&lt;t.d&lt;&lt;" , "&lt;&lt;t.s&lt;&lt;" , ";
      os&lt;&lt;t.vi;
      os&lt;&lt;" }"&lt;&lt;endl;
      return os;
  }
  
  int main()
  {
      Test test1;
      while (cin&gt;&gt;test1)
          cout&lt;&lt;test1;
  
  
  }
  </code></pre>
  
  <p>I have in main the <code>while (cin&gt;&gt;test1) cout&lt;&lt;test1</code> to read and print the structs.
  But as soon a read a second struct from cin I get the following:</p>
  
  <pre><code>./testin
  type the double d value: 1.0
  type the string s value: 1st struct string
  type the vector&lt;int&gt;elements :
  1
  1
  1
  { 1 , 1st struct string , [ 1 1 1  ] }
  type the double d value: 2.0
  type the string s value: 2nd struct string
  type the vector&lt;int&gt;elements :
  2
  2
  2
  { 2 , 2nd struct string , [ 1 1 1 2 2 2  ] }
  type the double d value:
  </code></pre>
  
  <p>The vector is mixed up, plus that I cannot stop the input with CTRL+d 
  I m able to read and print a single struct , If I have in main <code>cin&gt;&gt;test1;cout&lt;&lt;test1;</code>
  I looked for a proper solution  a lot , but I did not manage to figure it out.</p>
  
  <p>Thanks a lot for any help in advanced.</p>
  
  <p>snek</p>