14.Problem statement
Crete User defined exception to check the following conditions and throw the exception if the criterion does not meet. a. User has age between 18 and 55 b. User stays has income between Rs. 50,000 – Rs. 1,00,000 per month c. User stays in Pune/ Mumbai/ Bangalore / Chennai d. User has 4-wheeler Accept age, Income, City, Vehicle from the user and check for the conditions mentioned above. If any of the condition not met then throw the exception.
Program
#include<iostream>
#include<string.h>
using namespace std;
class user
{ public:
char vehicle;
int salary,age;
string city;
user()
{
age=0;
vehicle=0;
salary=0;
}
void getdata();
};
void user::getdata()
{
cout<<"Enter Age Of Person:- ";
cin>>age;
if(age<18 || age>55)
{
throw 1;
}
cout<<"Enter Salary For Permonth:- ";
cin>>salary;
if(salary<50000 || salary>100000)
{
throw 2;
}
cout<<"Do u have four wheeler(y/n):- ";
cin>>vehicle;
if(vehicle=='n')
{
throw 3;
}
cout<<"Enter the City name User stay:- :- ";
cin>>city;
if(city=="pune" || city=="mumbai" || city=="bangalore" || city=="chennai")
{}
else
{
throw 4;
}
}
int main()
{
user u;
try
{
u.getdata();
}
catch(int i)
{
switch(i)
{
case 1:
cout<<"\nInvalid....Please enter age between(18-55)";
break;
case 2:
cout<<"\n Invalid...Please enter salary between(50000-100000)";
break;
case 3:
cout<<"\nUser Donot have four vehicle";
break;
case 4:
cout<<"\nInvalid city";
break;
}
}
return 0;
}
Comments
Post a Comment