title: std::cin infinite loop for wrong input
body: <p>In my following code I want to loop until the user provided the right input. But when I tried it's turning into a nonstop loop.<br>
  <code>Please Enter Valid Input.</code><br>
  without the while loop it's also the same.</p>
  
  <p>Here with while loop:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;ctime&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  class library {
  public:
      library() {
          int mainOption;
  
          cout&lt;&lt;"Please choose the option you want to perform."&lt;&lt;endl;
          cout&lt;&lt;"1. Member Section"&lt;&lt;"\n"&lt;&lt;"2. Books, Lending &amp; Donate Section"&lt;&lt;"\n"&lt;&lt;"3. Returning Section"&lt;&lt;endl;
          bool option=true;
          while (option==true) {
              cin&gt;&gt;mainOption;
              if (mainOption==1) {
                  cout&lt;&lt;"section 1"&lt;&lt;endl;
                  option=false;
              } else if (mainOption==2) {
                  cout&lt;&lt;"section 1"&lt;&lt;endl;
                  option=false;
              } else if (mainOption==3) {
                  cout&lt;&lt;"section 1"&lt;&lt;endl;
                  option=false;
              } else {
                  cout&lt;&lt;"Please Enter Valid Input. "&lt;&lt;endl;
                  //option still true. so it should ask user input again right?
              }
          }
      }
  };
  
  int main(int argc, const char * argv[])
  {
      library l1;
      return 0;
  }
  </code></pre>
  
  <p>Here without the while loop. But same thing happening.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;ctime&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  class library {
  public:
      library() {
          int mainOption;
  
          cout&lt;&lt;"Please choose the option you want to perform."&lt;&lt;endl;
          cout&lt;&lt;"1. Member Section"&lt;&lt;"\n"&lt;&lt;"2. Books, Lending &amp; Donate Section"&lt;&lt;"\n"&lt;&lt;"3. Returning Section"&lt;&lt;endl;
  
          cin&gt;&gt;mainOption;
          if (mainOption==1) {
              cout&lt;&lt;"section 1"&lt;&lt;endl;
          } else if (mainOption==2) {
              cout&lt;&lt;"section 1"&lt;&lt;endl;
          } else if (mainOption==3) {
              cout&lt;&lt;"section 1"&lt;&lt;endl;
          } else {
              cout&lt;&lt;"Please Enter Valid Input. "&lt;&lt;endl;
              library();//Calling library function again to input again.
          }
      }
  };
  
  int main(int argc, const char * argv[])
  {
      library l1;
      return 0;
  }
  </code></pre>