title: How to avoid infinite loop when user inputs a character instead of float and integer?
body: <p>Im working on a program that will prompt the user for their name &amp; etc and voltage and current values to calculate power. It will then store all the data into a csv file.The problem is, my program goes into an infinite loop when the user inputs a character instead of integer or float for slot,lux,lux1,a(voltage),b(current)...How can I avoid this ?</p>
  
  <pre><code>//the program terminates when user enter -100 for voltage
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include&lt;string&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
      int main()
      {
          float a=0,b=0,c=0;
          int i,no=0,slot,lux=0,lux1=0;
          string name,id,date,time,lot,shift;
          ifstream indata;
          ofstream outdata;
  
          system("color 0A");
  
          cout&lt;&lt;"\n";
          cout&lt;&lt;"Enter Name:";
          cin&gt;&gt;name;
          cout&lt;&lt;"Enter Employee Number:";
          cin&gt;&gt;id;
          cout&lt;&lt;"Enter Date:";
          cin&gt;&gt;date;
          cout&lt;&lt;"Enter Time:";
          cin&gt;&gt;time;
          cout&lt;&lt;"Enter Lot No:";
          cin&gt;&gt;lot;
          cout&lt;&lt;"Enter Shift:";
          cin&gt;&gt;shift;
          cout&lt;&lt;"Enter Slot:";
          cin&gt;&gt;slot;//when User enter a character here,it goes into infinite loop
  
  
  
          cout&lt;&lt;"\n";
          cout&lt;&lt;"REMINDER:\n";
          cout&lt;&lt;"\n";
          cout&lt;&lt;"Center Cavity Lux Range is : 10500--10600\n";
          cout&lt;&lt;"Cavity 1 Lux Range is : 9500--9600\n";
          cout&lt;&lt;"\n";
          cout&lt;&lt;"Enter Center Cavity Lux Value:";
          cin&gt;&gt;lux;//when User enter a character here,it goes into infinite loop
  
          while((lux&lt;10500)||(lux&gt;10600)){
          cout&lt;&lt;"Incorrect Value.Please Enter the correct LUX value:";
          cin&gt;&gt;lux;
          }
  
          cout&lt;&lt;"Enter Cavity 1 Lux Range:";
          cin&gt;&gt;lux1;
  
          while((lux1&lt;9500)||(lux1&gt;9600)){
          cout&lt;&lt;"Incorrect Value.Please Enter the correct LUX value:";
          cin&gt;&gt;lux1;
          }
  
  
          outdata.open("Out.csv", ios::app);
  
          outdata&lt;&lt;"Solar Panel Test"&lt;&lt; endl;
          outdata&lt;&lt;"\n"&lt;&lt; endl;
          outdata&lt;&lt;"Name:"&lt;&lt;","&lt;&lt;name&lt;&lt; endl;
          outdata&lt;&lt;"Employee Number:"&lt;&lt;","&lt;&lt;id&lt;&lt; endl;
          outdata&lt;&lt;"Date:"&lt;&lt;","&lt;&lt;date&lt;&lt; endl;
          outdata&lt;&lt;"Time:"&lt;&lt;","&lt;&lt;time&lt;&lt; endl;
          outdata&lt;&lt;"Lot No:"&lt;&lt;","&lt;&lt;lot&lt;&lt; endl;
          outdata&lt;&lt;"Shift:"&lt;&lt;","&lt;&lt;shift&lt;&lt; endl;
          outdata&lt;&lt;"\n"&lt;&lt; endl;
          outdata&lt;&lt;"Center Cavity Lux Value:"&lt;&lt;","&lt;&lt;lux&lt;&lt; endl;
          outdata&lt;&lt;"Cavity 1 Lux Range:"&lt;&lt;","&lt;&lt;lux1&lt;&lt; endl;
  
          outdata&lt;&lt;","&lt;&lt;","&lt;&lt;","&lt;&lt; endl;
          outdata&lt;&lt;","&lt;&lt;","&lt;&lt;","&lt;&lt; endl;
          outdata &lt;&lt; "No,Slot,Voltage(V),Current(mA),Power(VmA)" &lt;&lt; endl;
  
          cout&lt;&lt;"\n";
          cout&lt;&lt;"Program Starts!\n";
          cout&lt;&lt;"\n";
  
          while (!(a ==-100))
       {
  
          cout&lt;&lt;"Enter VOLTAGE:";
          cin&gt;&gt;a;   //when User enter a character here,it goes into infinite loop
  
          while(((a&lt;0)||(a&gt;=5))&amp;&amp;(a!=-100))
          {
          cout&lt;&lt;"Incorrect Value.Please Enter the VOLTAGE :";
          cin&gt;&gt;a;
          }
  
          cout&lt;&lt;"Enter CURRENT:";
          cin&gt;&gt;b;  //when User enter a character here,it goes into infinite loop
  
          while((b&lt;0)||(b&gt;=17)){
          cout&lt;&lt;"Incorrect Value.Please Enter the CURRENT :";
          cin&gt;&gt;b;
          }
  
          c=a*b;
  
          if((a&gt;0)&amp;&amp;(a&lt;5))
           no++;
  
  
  
          if(c&lt;=25.4)
          {
            cout&lt;&lt;"\n";
            cout&lt;&lt;"|----   |---|     -----   |      \n";
            cout&lt;&lt;"|----  |-----|      |     |      \n";
            cout&lt;&lt;"|     |       |     |     |      \n";
            cout&lt;&lt;"|    |         |  -----   |____  \n";
            cout&lt;&lt;"\n\n\n";
          }
            else 
          {
            cout&lt;&lt;"\n";
            cout&lt;&lt;"|----    |---|    |----  |----     \n";       
            cout&lt;&lt;"|    |  |-----|   |____  |____     \n";
            cout&lt;&lt;"|----  |       |       |      |    \n";
            cout&lt;&lt;"|     |         |  ____|  ____|    \n";
            cout&lt;&lt;"\n\n\n";
  
          }
  
  
            outdata&lt;&lt;no&lt;&lt;","&lt;&lt;slot&lt;&lt;","&lt;&lt;a&lt;&lt;","&lt;&lt;b&lt;&lt;","&lt;&lt;c&lt;&lt; endl;  
  
       }
          indata.open("Out.csv");
  
          system("pause");
          return 0;
      }
  </code></pre>
  
  <p>I tried using this code for slot,lux,lux1,a(voltage),b(current):</p>
  
  <pre><code>while (!(cin &gt;&gt; slot))
      {
      cin.clear();
      cin.ignore(numeric_limits&lt;streamsize&gt;::max(),'\n');
      cout &lt;&lt; "Please input a proper number for slot: " &lt;&lt; endl;
      }
  </code></pre>
  
  <p>But now with this, I have to press each and every value of mine twice instead of once.
  Pls do help me !</p>