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

时间:2014-04-29 02:26:11

标签: c++ c++11

我正致力于一个程序,该程序将提示用户输入他们的名字和等等和电压和电流值来计算功率。然后它会将所有数据存储到一个csv文件中。问题是,当用户为插槽,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--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;
    }

我尝试将此代码用于slot,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;
    }

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

3 个答案:

答案 0 :(得分:2)

使用cin >>通常只适用于&#34;玩具&#34;程式。如果你想在现实世界中阅读用户输入&#34;方式,您需要使用getline()并将字符串转换/解析为您想要检查有效性的任何数据类型,即具有解析/检查整数,日期,时间等的特殊函数。

答案 1 :(得分:2)

你需要清除输入的错误状态,但也要删除错误的输入(它被检测为错误并激活错误标志但仍在那里等待)

你需要添加

    if (cin.fail()){
        cin.clear();
        cin.ignore(100,'\n'); // it will ignore 100 characters or get to the end of the line.
    }

在每个WHILE循环的开头,只需要获取数字。

这与您的代码的一般部分保持一致,但其他人建议使用更高级和安全的方法来执行此操作。

答案 2 :(得分:0)

isalphaisdigit等功能以及cctype中的其他功能非常有用。但是,正如另一个答案所述,您可以使用getline()。但是,我认为有一个替代方案会很好。