title: How to read a vector of strings from stdin in c++
body: <p>From the book "Accelerated C++":</p>
  
  <blockquote>
    <p>Ex 4-5-> Write a function that reads words from an input stream and stores them in a vector. Use that function both to write programs that count the number of words in the input, and to count how many times each word occurred.</p>
  </blockquote>
  
  <p>This is the code I am trying to run:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  using std::cin;       using std::cout;
  using std::vector;    using std::sort;
  using std::endl;      using std::string;
  using std::istream;
  
  istream&amp; read_words(istream&amp; in, vector&lt;string&gt;&amp; words) {
    if (in) {
      words.clear();
      string word;
  
      while (in &gt;&gt; word)
        words.push_back(word);
  
      in.clear();
    }
  
    return in;
  }
  
  int main() {
    vector&lt;string&gt; words;
  
    read_words(cin, words);
  
    cout &lt;&lt; "Num of words: " &lt;&lt; words.size() &lt;&lt; endl;
  
    sort(words.begin(), words.end());
  
    string prev_word = "";
    int count = 0;
  
    for (vector&lt;string&gt;::size_type i = 0; i &lt; words.size(); ++i) {
      if (words[i] != prev_word) {
        if (prev_word != "")
           cout &lt;&lt; prev_word &lt;&lt; " appeared " &lt;&lt; count &lt;&lt; " times" &lt;&lt; endl;
  
        prev_word = words[i];
        count = 1;
      }
      else
        ++count;
    }
  
    cout &lt;&lt; prev_word &lt;&lt; " appeared " &lt;&lt; count &lt;&lt; " times" &lt;&lt; endl;
  
    return 0;
  }
  </code></pre>
  
  <p>Now, when I try to run the code, it gets stuck at the input and keeps reading 
  until I abort the program using <kbd>Ctrl+C</kbd>. What is wrong with my code?</p>