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

Validating integer input from user in c++

我的程序要求用户输入年龄,但如果有一些字符输入是 int age,我必须验证年龄,然后我必须抛出异常并要求用户再次输入年龄。 .

在我的程序中,我首先调用 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;
}

cin处于错误状态时,您需要使用cin.clear()cin.ignore,将您的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处于错误状态时,您必须清除它,然后再使用它。请参阅 this post 关于 cin.fail()cin.ignore()