title: Why does cin.clear() fix an infinite loop caused by bad input to cin?
body: <p>I've written a switch statement, and created a default which would simply say the user picked a bad option and repeat the input. I wanted to make sure that if there was an issue, it would clear the buffer first, so I used cin.sync(), but entering an 'a' for the input still caused an infinite loop. I added cin.clear() to clear the flags which gave me working code ... but my confusion is why it worked. Does cin.sync() not work if there is a fail flag?</p>
  
  <p>the statement follows, truncated for brevity:</p>
  
  <pre><code>while(exitAllow == false)
  {
      cout&lt;&lt;"Your options are:\n";
      cout&lt;&lt;"1: Display Inventory\n";
      /*truncated*/
      cout&lt;&lt;"5: Exit\n";
      cout&lt;&lt;"What would you like to do? ";
  
      cin.clear();   //HERE IS MY CONFUSION//
      cin.sync();
      cin&gt;&gt;action;
      switch(action)
      {
          case 1:
                      /*truncated*/
          case 5:
              exitAllow = true;
              break;
          default:
              cout&lt;&lt;"\ninvalid entry!\n";
              break;
      }
  }
  </code></pre>