title: C++ cin.fail() executes and moves to next line even though input is of another data type
body: <p>I am using get.fail() to check if there's any char in the input, and if there is I would like to give the user a chance to re-enter. However, the program seems to still accept the user input whenever there's an integer in front of the input no matter the case. Say <code>w1</code> and <code>1w</code>, the program will tell the user that the it only accepts integers while the latter one accepts the input and moves over to the next line which then causes another problem.</p>
  
  <pre><code>    void userChoice(int input){
  
          switch(input)
          {
              case 1:
                  insert();
                  break;
              case 2:
                  display();  
                  break;
              case 3:
                  update_data();
                  break;
              case 4:
                  delete_position();
                  break;
              case 5:
                  cout&lt;&lt;"Thank you for using the program\n";
                  exit(0);
                  break;
              case 6:
                  tellSize();
                  break;
              default:
                  cout&lt;&lt;"Not an option\n";
                  cin&gt;&gt;input;
  
                  while(cin.fail())
                  {
                      cin.clear();
                      cin.ignore(INT_MAX, '\n'); 
                      cin&gt;&gt;input;
                      break; 
                  }
                  userChoice(input);
          }
  
      }
  </code></pre>
  
  <p>Referring to the code above, say I give the input <code>1w</code>. The program will still execute case 1 as if there's nothing wrong, and then <code>w</code> is somehow passed into the <code>insert()</code> function which is not what I want. I would like the program to let the user re-enter the input no matter if it's <code>1w</code> or <code>w1</code>, in short I do not want the program to move over to the next line if there's a char in an integer input. </p>
  
  <p>tl;dr: 
  Why does the code below still execute when the cin is <code>1w</code>, shouldn't it print "Enter number only" since there's a character in there?</p>
  
  <p>Edit: Here's a quick program I made, to reproduce the error I am facing, I first enter <code>1h</code> and here's the first bug I'm facing, why is the program still executing when there's a char <code>h</code> in the input? Afterwards in the second input, I enter <code>2w</code> and the program prints out <code>2</code>, shouldn't the program loop the while loop since there's a char <code>w</code> in the input?</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;iomanip&gt;
  #include&lt;limits&gt;
  using namespace std;
  
  void option_1()
  {
      int amount_input;
  
      cout&lt;&lt;"Enter the amount of cake"&lt;&lt;endl;
      cin&gt;&gt;amount_input;
      while(cin.fail())
      {
          cin.clear();
          cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n'); 
          cout&lt;&lt;"Enter number only\n";
          cin&gt;&gt;amount_input;
      }
          cout&lt;&lt;amount_input;
  }
  
  void options(int input)
  {
      bool fail;
  
      switch(input)
      {
          case 1:
              option_1();
              break;
  
  
          default:
              cout&lt;&lt;"Not an option"&lt;&lt;endl;
              cin&gt;&gt;input;
              while(cin.fail())
              {
                  cin.clear();
                  cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n'); 
                  cin&gt;&gt;input;
                  break; 
              }
              options(input);
  
      }
  }
  
  void menu(){
      int user_input;
  
      cout&lt;&lt;"Enter 1 to print something\n";
      cin&gt;&gt;user_input;
      options(user_input);
  }
  
  int main(){
      menu();
  }
  </code></pre>