why does my cin loop never end?
<p>In the following code, if the user inputs something that is not an <code>int</code>, the program goes into an infinite loop.  Why does this happen, and what should I do to fix it?</p>

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


int main()
{
    int i;
    char str[100];
    while (!(cin &gt;&gt; i))
    {
        gets(str);
        cout &lt;&lt; "failure read!" &lt;&lt; endl;
    }

    cout &lt;&lt; "successful read!" &lt;&lt; endl;
    return 0;
}
</code></pre>
