Crete User defined exception to check the following conditions and throw the exception if the criterion does not meet

/*

Problem statement:
Create 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.
*/
#include<iostream>

using namespace std;
//class test
class test
{
  float age,salary;
  int vehicle;
  string city;
  public:
       
        void get() ;
        void display();       
};
//get function
void test::get()
{
   cout<<"\n Enter age";
   cin>>age;
   cout<<"\n Enter salary";
   cin>>salary;
   cout<<"\n Enter city:";
   cin>>city;
   cout<<"\n Enter Vehicle type(2/4):";
   cin>>vehicle;
//try block
   try

    {
       if(age>18 && age <55 )//checking age condition
          {
            if(salary > 50000 && salary < 100000)  //checking salary condition
                 {
   if(city=="Pune" || city== "Mumbai" || city=="Banglore" || city=="Chennai") //checking city condition
                         {
    if(vehicle==4)
                                 display();
                           
  }

                 }
          }
      else throw(vehicle);//throwing exception
    }
   catch(int i){cout<<"\n Caught Exception in get function";throw;}
 
}
//display function
void test::display() 
{
  cout<<"\n Age is :"<<age<<endl;
  cout<<"\n Salary is:"<<salary<<endl;
  cout<<"\n City is:"<<city;
  cout<<"\n Vehicle is:" <<vehicle;
}

int main()
{
test e1;
  try
     {
         
          e1.get();
       
      }
   catch(...){cout<<"\n Caught exception in main\n";} 
 
}

/*
Output:


 Enter age12

 Enter salary40000

 Enter city:pune

 Enter Vehicle type(2/4):2

 Caught Exception in get function
 Caught exception in main
comp-sys-14@comp-sys-14:~/Desktop/oop_final$ ./a.out

 Enter age22

 Enter salary60000

 Enter city:Pune

 Enter Vehicle type(2/4):4

 Age is :22

 Salary is:60000

 City is:Pune
 Vehicle is:4comp-sys-14@comp-sys-14:~/Desktop/oop_final$
*/

Post a Comment

0 Comments