Do While loop breaks after incorrect input?
<p>I am trying to have a loop continue to prompt the user for an option. When I get a string of characters instead of an int, the program loops indefinitely. I have tried setting the variable result to NULL, clearing the input stream, and have enclosed in try{}catch blocks (not in this example). Can anyone explain to me why this is?</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

using namespace std;

int menu(string question, vector&lt;string&gt; options)
{
    int result;

    cout &lt;&lt; question &lt;&lt; endl;

    for(int i = 0; i &lt; options.size(); i++)
    {
        cout &lt;&lt; '[' &lt;&lt; i &lt;&lt; ']' &lt;&lt; options[i] &lt;&lt; endl;
    }

    bool ans = false;
    do
    {
        cin &gt;&gt; result;
        cin.ignore(1000, 10);
        if (result &lt; options.size() )
        {
            ans = true;
        }
        else
        {
            cout &lt;&lt; "You must enter a valid option." &lt;&lt; endl;
            result = NULL;
            ans = false;
        }       
    }
    while(!ans);
    return result;
}

int main()
{
    string menuQuestion = "Welcome to my game. What would you like to do?";
    vector&lt;string&gt; mainMenu;
    mainMenu.push_back("Play Game");
    mainMenu.push_back("Load Game");
    mainMenu.push_back("About");
    mainMenu.push_back("Exit");

    int result = menu(menuQuestion, mainMenu);
    cout &lt;&lt; "You entered: " &lt;&lt; result &lt;&lt; endl;
    return 0;
}
</code></pre>
