簡體   English   中英

在 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處於錯誤狀態時,您必須在重新使用它之前清除它。 請參閱上后cin.fail()cin.ignore()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM