当用户输入一个字符而不是浮点和整数时,如何避免无限循环

How to avoid infinite loop when user inputs a character instead of float and integer?

本文关键字:整数 无限循环 何避免 输入 用户 字符 一个      更新时间:2023-10-16

我正在开发一个程序,该程序将提示用户输入他们的姓名&以及电压和电流值来计算功率。然后,它会将所有数据存储到csv文件中。问题是,当用户输入一个字符而不是整数或float来表示slot、lux、lux1、a(电压(、b(电流(时,我的程序会进入无限循环。。。我该如何避免这种情况?

//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--10600n";
        cout<<"Cavity 1 Lux Range is : 9500--9600n";
        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<<"nnn";
        }
          else 
        {
          cout<<"n";
          cout<<"|----    |---|    |----  |----     n";       
          cout<<"|    |  |-----|   |____  |____     n";
          cout<<"|----  |       |       |      |    n";
          cout<<"|     |         |  ____|  ____|    n";
          cout<<"nnn";
        }

          outdata<<no<<","<<slot<<","<<a<<","<<b<<","<<c<< endl;  
     }
        indata.open("Out.csv");
        system("pause");
        return 0;
    }

我试着将这个代码用于插槽、lux、lux1、a(电压(、b(电流(:

while (!(cin >> slot))
    {
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(),'n');
    cout << "Please input a proper number for slot: " << endl;
    }

但现在有了这个,我必须把我的每一个值都按两次,而不是一次。请帮帮我!

使用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循环开始时,只需要获取数字。

这与代码的一般行保持一致,但其他人在这里建议了更高级、更安全的方法。

cctype中的isalpha、isdigit等函数非常有用。但是,正如另一个答案所述,您可以使用getline()。然而,我认为有一个替代方案会很好。