C++ 当用户输入字符而不是浮点和整数时,如何避免无限循环?
我正在开发一个程序,该程序将提示用户输入他们的姓名等以及电压和电流值,以计算功率。然后它会将所有数据存储到一个csv文件中。问题是,当用户输入一个字符而不是插槽、lux、lux1、avoltage、bccurrent的整数或浮点时,我的程序会进入一个无限循环……我如何避免这种情况C++ 当用户输入字符而不是浮点和整数时,如何避免无限循环?,c++,c++11,C++,C++11,我正在开发一个程序,该程序将提示用户输入他们的姓名等以及电压和电流值,以计算功率。然后它会将所有数据存储到一个csv文件中。问题是,当用户输入一个字符而不是插槽、lux、lux1、avoltage、bccurrent的整数或浮点时,我的程序会进入一个无限循环……我如何避免这种情况 //the program terminates when user enter -100 for voltage #include <iostream> #include <fstream>
//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;
}
我尝试将此代码用于插槽、lux、lux1、avoltage和bcurrent:
while (!(cin >> slot))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Please input a proper number for slot: " << endl;
}
但是现在有了这个,我必须将我的每个值按两次而不是一次。
请帮帮我 使用cin>>通常仅用于玩具程序。如果您想以真实的方式读取用户输入,则需要使用getline并将字符串转换/解析为您希望随时检查有效性的任何数据类型,即具有解析/检查整数、日期、时间等的特殊函数。使用cin>>通常仅适用于玩具程序。如果您希望以真实的方式读取用户输入,则需要使用getline并将字符串转换/解析为您希望随时检查有效性的任何数据类型,即,具有解析/检查整数、日期、时间的特殊函数,等等。您需要清除输入的错误状态,但也要删除错误输入,因为它被检测为错误并激活了错误标志,但仍在等待 您需要添加
if (cin.fail()){
cin.clear();
cin.ignore(100,'\n'); // it will ignore 100 characters or get to the end of the line.
}
在每次WHILE循环开始时,只需要获取数字
这与您的代码的一般行保持一致,但其他人在这里建议了更高级和更安全的方法 您需要清除输入的错误状态,但也要删除错误输入,因为它被检测为错误并激活了错误标志,但仍在等待 您需要添加
if (cin.fail()){
cin.clear();
cin.ignore(100,'\n'); // it will ignore 100 characters or get to the end of the line.
}
在每次WHILE循环开始时,只需要获取数字
这与您的代码的一般行保持一致,但其他人在这里建议了更高级和更安全的方法 中的函数如和其他函数都很有用。但是,正如另一个答案所述,您可以使用getline。但是,我认为最好有一个替代方案。中的函数如和其他函数都很有用。但是,正如另一个答案所述,您可以使用getline。但是,我认为最好有一个替代方案。这是您的代码吗?我想这就是它的意义所在。当用户输入一些无效值时,它应该发出警报,让用户再次输入。@YuchenZhong:是的,但它不会使用无效的输入,这会导致无限循环。@YuchenZhong:好的..那么我如何在代码中解决这个问题?@YuchenZhong:这是我的代码…正是!当用户输入无效值(如字符)时,我想提示用户,直到他/她输入有效的整数或浮点。那么我如何在代码中解决此问题?这是您的代码吗?我想这就是它的意义所在。当用户输入一些无效值时,它应该发出警报,让用户再次输入。@YuchenZhong:是的,但它不会使用无效的输入,这会导致无限循环。@YuchenZhong:好的..那么我如何在代码中解决这个问题?@YuchenZhong:这是我的代码…正是!当用户输入字符等无效值时,我希望提示用户输入有效的整数或浮点。那么如何在代码中解决此问题?