title: Using a string stream to fill a vector of integers
body: <p>This is a code snippet from a program that asks for inputs in a loop which is pretty bulky and hard to sift through so I limited my post to this snippet. This should be reading the inputs of some number of integers into the vector iin.
  For some reason, when the program completes its first loop, the next loop which asks for a character, recieves the last few numbers that were entered in the first loop. I take the inputs at the beginning of the loop with cin, so somehow the end of my string stream is going into cin at the beginning of the next loop?
  Not sure where to go from here.</p>
  
  <p>I have looked at many many posts here that execute this same strategy but I can't see any part of those posts which I have missed.</p>
  
  <pre><code>        cout &lt;&lt; "Please enter command parameters: ";
          string in;
          int temp;
          cin &gt;&gt; in;
          istringstream input(in);
          while(input&gt;&gt;temp){
              iin.push_back(temp);
              input.clear();
          }
  </code></pre>
  
  <p>Sorry for not making a proper example code.</p>
  
  <p>Here:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Functions.h"
  #include &lt;fstream&gt;
  #include &lt;cstring&gt;
  #include &lt;vector&gt;
  #include &lt;sstream&gt;
  using namespace std;
  bool quit=false,filewrite=false,filein=false;
  char cmd;
  std::vector&lt;int&gt; iin;
  std::vector&lt;double&gt; din;
  
  
  extern ofstream ofile;
  extern ifstream ifile;
  
  int main(){
      while(!quit){
          cout &lt;&lt; "Please enter command code: ";
          cin &gt;&gt; cmd;
          cout &lt;&lt; "Please enter command parameters: ";
          string in;
          int temp;
          cin &gt;&gt; in;
          istringstream input(in);
          while(input&gt;&gt;temp){
              iin.push_back(temp);
              input.clear();
          }
          // call functions after, one of them will trigger quit
  }
  }
  </code></pre>
  
  <p>Running this code results in <a href="https://i.stack.imgur.com/NsW1U.png" rel="nofollow noreferrer">this</a> for me.</p>
  
  <p>Sorry I don't have the ability to embed yet.</p>
  
  <p>OK! I have resolved it.</p>
  
  <p>Realized my inputs have spaces so in was only ever recieving first character, as was pointed out in the comments. This:</p>
  
  <pre><code>            cout &lt;&lt; "Please enter command parameters: ";
              char inp[256];
              int temp;
              cin.getline(inp,256);
              string strin(inp);
              istringstream ss(strin);
              while(ss&gt;&gt;temp){
                  cout &lt;&lt; temp &lt;&lt; endl;
                  iin.push_back(temp);
              }
  </code></pre>
  
  <p>Gives me the right result. Initially this method didn't work because I had used cin previously, which resulted in me needing to use cin.ignore() to clear the buffer. Not too sure on this explanation since I just barely came up with the solution but hopefully it's useful to someone. Cheers!</p>