在C ++中验证用户的整数输入

时间:2018-10-27 05:57:59

标签: c++ validation c++11 exception exception-handling

我的程序要求用户输入年龄,但是我必须验证年龄,如果int age中有一些字符输入,那么我必须抛出异常并要求用户再次输入年龄... < / p>

在我的程序中,我首先调用inputAge()函数,询问用户年龄,然后检查cin是否失败,然后抛出字符串“ invalid”,并在catch块中调用inputAge ()再次起作用,但进入无限循环。

请有人告诉我我错了或为此要做什么......

谢谢!

#include<exception>
#include<iostream>
using namespace std;
class MyException: public exception {
    string msg;
public:
    MyException() {

    }
    void setError(string msg) {
        this->msg=msg;
    }
    const char* what() {
        return msg.c_str();
    }
};
class UserData {
    int age;
    long income;
    string city;
    int wheeler;
public:
    void inputAge() {
        try {
            cout<<"Enter age: ";
            cin>>age;
            if(!cin) {
                throw "invalid";
            }
            else {
                if(age < 18 || age > 55) {
                    MyException e;
                    e.setError("User has age between 18 and 55 ");
                    throw e;
                }
            }
        }catch(MyException &e) {
            cout<<e.what()<<endl;
            inputAge();
        }
        catch(const char* msg) {
            cout<<msg;
            inputAge();
        }
    }
    void inputIncome() {
        try {
            cout<<"Enter income: ";
            cin>>income;
            if(income < 50000 || income > 100000) {
                MyException e;
                e.setError("User has income between Rs. 50,000 – Rs. 1,00,000 per month");
                throw e;
            }
        }
        catch(MyException &e) {
            cout<<e.what()<<endl;
            inputIncome();
        }
    }
    void inputCity() {
        try {
            cout<<"Enter city with first letter capital: ";
            cin>>city;
            if(city != "Pune" && city != "Mumbai" && city != "Bangalore" && city != "Chennai") {
                MyException e;
                e.setError("User stays in Pune/Mumbai/Bangalore/Chennai");
                throw e;
            }
        }
        catch(MyException &e) {
            cout<<e.what()<<endl;
            inputCity();
        }
    }
    void inputVehicle() {
        try {
            cout<<"Enter vehicle (2-wheeler or 4- wheeler): ";
            cin>>wheeler;
            if(wheeler == 2) {
                MyException e;
                e.setError("User must have 4 wheeler");
                throw e;
            }
        }
        catch(MyException &e) {
            cout<<e.what()<<endl;
            inputVehicle();
        }
    }
    void display() {
        cout<<"****User details****"<<endl;
        cout<<"Age: "<<age<<endl;
        cout<<"Income: "<<income<<endl;
        cout<<"City: "<<city<<endl;
        cout<<"vehicle: "<<wheeler<<" wheeler"<<endl;
    }
};
int main() {
    UserData ud;
    ud.inputAge();
    ud.inputIncome();
    ud.inputCity();
    ud.inputVehicle();
    ud.display();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

cin.clear()处于错误状态时,您需要使用cin.ignorecin,将inputAge()函数更改为:

    void inputAge() {
    try {
        cout<<"Enter age: ";
        cin>>age;
        if(cin.fail()) {
            cin.clear(); //YOU MUST CLEAR THE ERROR STATE
            cin.ignore();
            throw "invalid";
        }
        else {
            if(age < 18 || age > 55) {
                MyException e;
                e.setError("User has age between 18 and 55 ");
                throw e;
            }
        }
    }catch(MyException &e) {
        cout<<e.what()<<endl;
        inputAge();
    }
    catch(const char* msg) {
        cout<<msg;
        inputAge();
    }
}

std::cin处于错误状态时,您 必须 将其清除,然后重新使用。请参阅cin.fail()cin.ignore()上的this帖子