vendredi 26 octobre 2018

Validating integer input from user in c++

My program ask the user to enter the age, but i have to validate the age if there is some char input comes in int age then i have to throw the exception and ask the user to enter the age again...

In my program I'm firstly call inputAge() function that ask for user age after that i check the cin if it is fail then I'm throw the string "invalid" and in catch block I'm calling the inputAge() function again but it goes to infinite loop.

Please someone tell me where i'm wrong or what i have to do for this....

Thanks in advance!

#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;
}

Aucun commentaire:

Enregistrer un commentaire