title: cin to vector in C++
body: <p>This is how my program works. It prompts for user input, once non-digit detected, the loop will stop. Here is my code :</p>
  
  <pre><code>int size = 0;
  float number;
  float total = 0;
  vector &lt;float&gt; data;
  
  //prompt user to enter file name
  string file;
  cout &lt;&lt; "Enter a file name : " ;
  cin &gt;&gt; file ;
  //concatenate the file name as text file
  file += ".txt";
  
  //Write file
  cout &lt;&lt; "Enter number : ";
  ofstream out_file;
  out_file.open(file);
  while(cin &gt;&gt; number)
  {
      data.push_back(number);
      size++;
  }
  
  cout&lt;&lt; "Elements in array are : " ;
  //check whether is there any 0 in array else print out the element in array
  for (int count = 0; count &lt; size; count++)
  {
      if (data.size() == 0)
      {
          cout &lt;&lt; "0 digit detected. " &lt;&lt; endl;
          system("PAUSE");
      }else
      {
          //write the element in array into text file
          out_file &lt;&lt; data.size() &lt;&lt; " " ;
          cout &lt;&lt; data.size() &lt;&lt; " ";
      }
  }
  out_file.close();
  </code></pre>
  
  <p>However, there is some error. For example, I entered 1,2,3,4,5,g, it supposed to write the array as 1,2,3,4,5 into a text file. However, it written in 5,5,5,5,5 instead. I wonder am I using the push_back wrongly? Any guides would be appreciated.</p>
  
  <p>Thanks in advance.</p>