end of istream not detected when expected
<p>I wrote a function to count vowels. If there is a vowel at the end of the stream it gets counted twice. Why?</p>

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

void countChars(istream&amp; in, string theChars, ostream&amp; out) {
  map&lt;char, int&gt; charMap;
  map&lt;char, int&gt;::iterator mapIt;

  for (string::iterator i = theChars.begin(); i != theChars.end(); ++i) {
   charMap[*i] = 0;
  }
  while (in) {
    char c;
    in &gt;&gt; c;
    c = tolower(c);
    if (charMap.count(c))
      ++charMap[c];
  }
  for (mapIt = charMap.begin(); mapIt != charMap.end(); ++mapIt) {
    out &lt;&lt; (*mapIt).first &lt;&lt; ":" &lt;&lt; (*mapIt).second &lt;&lt; endl;
  }
}

int main(int argc, char **argv) {
  std::string s = "aeiou";
  countChars(std::cin, s, std::cout);
}
</code></pre>
