Create User defined exception to check the different conditions on age, income, city and vehicle and throw the exception if the
criterion does not meet.
Problem Solution:
#include<iostream>
#include<string.h>
using namespace std;
class user
{
int age,count;
char fw,city[10];
double income;
public:
user()
{
count=0;
}
void getage();
void getcity();
void getincome();
void getfw();
void print();
};
void user::getage()
{
cout<<"\nenter the age=";
cin>>age;
try
{
if(age>=18&&age<=55)
count++;
else
throw(age);
}
catch (int i)
{
cout<<"\nexception occured:age must be in between 18 to 55";
}
}
void user::getcity()
{
cout<<"\nenter city=";
cin>>city;
try
{
if(strcmp (city,"pune")==0&&strcmp (city,"banglore")&& strcmp (city,"mumbai")&&strcmp (city,"chennai"))
count++;
else
throw (city);
}
catch (char c[10])
{
cout<<"\nexception occured:city must pune,mumbai,banglore,chennai";
}
}
void user::getincome()
{
cout<<"\nenter the income=";
cin>>income;
try
{
if(income>=50000&&income<=100000)
count++;
else
throw (income);
}
catch(double j)
{
cout<<"\nexception occured:income must be in between 50000 to 100000";
}
}
void user::getfw()
{
cout<<"\ndo you own fw=";
cin>>fw;
try
{
if(fw=='Y'||fw=='y')
count++;
else
throw(fw);
}
catch(char k)
{
cout<<"\nexception occure:user must own fw";
}
}
void user::print()
{
cout<<"\nage="<<age;
cout<<"\ncity="<<city;
cout<<"\nincome="<<income;
cout<<"\nfw="<<fw;
cout<<"\ncount="<<count;
}
int main()
{
user u;
u.getage();
u.getcity();
u.getincome();
u.getfw();
u.print();
return 0;
}
criterion does not meet.
USER DEFINED EXCEPTION |
Problem Solution:
#include<iostream>
#include<string.h>
using namespace std;
class user
{
int age,count;
char fw,city[10];
double income;
public:
user()
{
count=0;
}
void getage();
void getcity();
void getincome();
void getfw();
void print();
};
void user::getage()
{
cout<<"\nenter the age=";
cin>>age;
try
{
if(age>=18&&age<=55)
count++;
else
throw(age);
}
catch (int i)
{
cout<<"\nexception occured:age must be in between 18 to 55";
}
}
void user::getcity()
{
cout<<"\nenter city=";
cin>>city;
try
{
if(strcmp (city,"pune")==0&&strcmp (city,"banglore")&& strcmp (city,"mumbai")&&strcmp (city,"chennai"))
count++;
else
throw (city);
}
catch (char c[10])
{
cout<<"\nexception occured:city must pune,mumbai,banglore,chennai";
}
}
void user::getincome()
{
cout<<"\nenter the income=";
cin>>income;
try
{
if(income>=50000&&income<=100000)
count++;
else
throw (income);
}
catch(double j)
{
cout<<"\nexception occured:income must be in between 50000 to 100000";
}
}
void user::getfw()
{
cout<<"\ndo you own fw=";
cin>>fw;
try
{
if(fw=='Y'||fw=='y')
count++;
else
throw(fw);
}
catch(char k)
{
cout<<"\nexception occure:user must own fw";
}
}
void user::print()
{
cout<<"\nage="<<age;
cout<<"\ncity="<<city;
cout<<"\nincome="<<income;
cout<<"\nfw="<<fw;
cout<<"\ncount="<<count;
}
int main()
{
user u;
u.getage();
u.getcity();
u.getincome();
u.getfw();
u.print();
return 0;
}
Post a Comment