Im working on a program that will prompt the user for their name & 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 ?
//the program terminates when user enter -100 for voltage
#include <iostream>
#include <fstream>
#include<string>
#include <iomanip>
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<<"\n";
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Employee Number:";
cin>>id;
cout<<"Enter Date:";
cin>>date;
cout<<"Enter Time:";
cin>>time;
cout<<"Enter Lot No:";
cin>>lot;
cout<<"Enter Shift:";
cin>>shift;
cout<<"Enter Slot:";
cin>>slot;//when User enter a character here,it goes into infinite loop
cout<<"\n";
cout<<"REMINDER:\n";
cout<<"\n";
cout<<"Center Cavity Lux Range is : 10500--10600\n";
cout<<"Cavity 1 Lux Range is : 9500--9600\n";
cout<<"\n";
cout<<"Enter Center Cavity Lux Value:";
cin>>lux;//when User enter a character here,it goes into infinite loop
while((lux<10500)||(lux>10600)){
cout<<"Incorrect Value.Please Enter the correct LUX value:";
cin>>lux;
}
cout<<"Enter Cavity 1 Lux Range:";
cin>>lux1;
while((lux1<9500)||(lux1>9600)){
cout<<"Incorrect Value.Please Enter the correct LUX value:";
cin>>lux1;
}
outdata.open("Out.csv", ios::app);
outdata<<"Solar Panel Test"<< endl;
outdata<<"\n"<< endl;
outdata<<"Name:"<<","<<name<< endl;
outdata<<"Employee Number:"<<","<<id<< endl;
outdata<<"Date:"<<","<<date<< endl;
outdata<<"Time:"<<","<<time<< endl;
outdata<<"Lot No:"<<","<<lot<< endl;
outdata<<"Shift:"<<","<<shift<< endl;
outdata<<"\n"<< endl;
outdata<<"Center Cavity Lux Value:"<<","<<lux<< endl;
outdata<<"Cavity 1 Lux Range:"<<","<<lux1<< endl;
outdata<<","<<","<<","<< endl;
outdata<<","<<","<<","<< endl;
outdata << "No,Slot,Voltage(V),Current(mA),Power(VmA)" << endl;
cout<<"\n";
cout<<"Program Starts!\n";
cout<<"\n";
while (!(a ==-100))
{
cout<<"Enter VOLTAGE:";
cin>>a; //when User enter a character here,it goes into infinite loop
while(((a<0)||(a>=5))&&(a!=-100))
{
cout<<"Incorrect Value.Please Enter the VOLTAGE :";
cin>>a;
}
cout<<"Enter CURRENT:";
cin>>b; //when User enter a character here,it goes into infinite loop
while((b<0)||(b>=17)){
cout<<"Incorrect Value.Please Enter the CURRENT :";
cin>>b;
}
c=a*b;
if((a>0)&&(a<5))
no++;
if(c<=25.4)
{
cout<<"\n";
cout<<"|---- |---| ----- | \n";
cout<<"|---- |-----| | | \n";
cout<<"| | | | | \n";
cout<<"| | | ----- |____ \n";
cout<<"\n\n\n";
}
else
{
cout<<"\n";
cout<<"|---- |---| |---- |---- \n";
cout<<"| | |-----| |____ |____ \n";
cout<<"|---- | | | | \n";
cout<<"| | | ____| ____| \n";
cout<<"\n\n\n";
}
outdata<<no<<","<<slot<<","<<a<<","<<b<<","<<c<< endl;
}
indata.open("Out.csv");
system("pause");
return 0;
}
I tried using this code for slot,lux,lux1,a(voltage),b(current):
while (!(cin >> slot))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Please input a proper number for slot: " << endl;
}
But now with this, I have to press each and every value of mine twice instead of once. Pls do help me !