cin stream infinite loop, why?
<p>I copied this simple program from c++ programming language, but I cannot get it work as desired. Am I missing something?
Basically, the program will output "input end" after I hit return, and then repeat input from cin. It is never able to go to the next statement. I tried to use the vector (commented two statements below), the same. Tried on Vc6 and vs2008.</p>

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

map&lt;string, int&gt; histogram;
void record(const string &amp;s)
{
    histogram[s]++; //this is pretty strange, however it does work!
    cout&lt;&lt;"recorded:"&lt;&lt;s&lt;&lt;" occurance="&lt;&lt;histogram[s]&lt;&lt;"\n";
}

void print(const pair&lt;const string,int&gt; &amp;r)
{
    cout&lt;&lt;r.first&lt;&lt;' '&lt;&lt;r.second&lt;&lt;'\n';
}

int main()
{
    istream_iterator&lt;string&gt; ii(cin);
    istream_iterator&lt;string&gt; eos;
    cout&lt;&lt;"input end\n";

    for_each(ii,eos,record); //this statement cannot get out why? It repeats the keyboard input
    //vector&lt;string&gt; b(ii,eos);
    //for_each(b.begin(),b.end(),record);
    for_each(histogram.begin(),histogram.end(),print); //program never comes here why?
}
</code></pre>

<p>Running results:</p>

<p>a b c</p>

<p>input end</p>

<p>recorded:a occurance=1</p>

<p>recorded:b occurance=1</p>

<p>recorded:c occurance=1</p>

<p>1 2 3</p>

<p>recorded:1 occurance=1</p>

<p>recorded:2 occurance=1</p>

<p>recorded:3 occurance=1</p>
