Exception Handling
Task 1:
Write a program that prompts the user to enter a length in
feet and inches and outputs the equivalent length in centimeters. If the user
enters a negative number, throw and handle an appropriate exception and prompt
the user to enter another set of numbers.
#include<iostream>
using namespace std;
class A{
double feet, inches;
public:
void in()
{
cout << "Enter the length in feet:";
cin >> feet;
cout << "Enter the length in inches:";
cin >> inches;
}
void cent()
{
try{
if (feet > 0)
{
cout << "Feets in centimeters are:" << feet*30.48 << endl;
}
else
{
throw feet;
}
if (inches > 0)
{
cout << "inches in centimeters are:" << inches*2.54 << endl;
}
else
{
throw inches;
}
}
catch (...)
{
cout << "there is any negative value" << endl;
}
}
};
int main()
{
A a1;
a1.in(); a1.cent();
getchar();
getchar();
return 0;
}
Task 2:
Write a program that prompts the user to enter a person’s date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDayand invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDayobject. Similar conventions for the invalid values of month and year.
#include<iostream>
using namespace std;
class DOB{
public:
int day, month, year;
DOB()
{
}
DOB(int a, int b, int c)
{
day = a; month = b; year = c;
}
};
class invalidDay{
public:
invalidDay()
{
}
void check1(DOB &d1)
{
try
{
if ((d1.day< 0) && (d1.day> 31))
throw d1.day;
cout << "We are throwing days" << endl;
}
catch (...)
{
cout << "Day is out from range" << endl;
}
}
};
class invalidMonth{
public:
invalidMonth()
{
}
void check(DOB &d1)
{
try{
if (d1.month<0 && d1.month>12)
throw d1.month;
else
check2(d1);
}
catch (...)
{
cout << "Month is invalid" << endl;
}
}
void check2(DOB &d1)
{
if (d1.month == 1)
{
cout << "Date of birth is: January " << d1.day << " " << d1.year << endl;
}
else if (d1.month == 2)
{
if (d1.day > 0 && d1.day <= 29)
{
if (d1.day == 29)
{
cout << "This is leap year" << endl;
cout << endl << endl;
}
cout << "Date of birth is: Fabuaray " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 3)
{
if (d1.day > 0 && d1.day <= 31)
{
cout << "Date of birth is: March " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 4)
{
if (d1.day > 0 && d1.day <= 30)
{
cout << "Date of birth is: April " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 5)
{
if (d1.day > 0 && d1.day <= 31)
{
cout << "Date of birth is: May " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 6)
{
if (d1.day > 0 && d1.day <= 30)
{
cout << "Date of birth is: june " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 7)
{
if (d1.day > 0 && d1.day <= 31)
{
cout << "Date of birth is: July " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 8)
{
if (d1.day > 0 && d1.day <= 31)
{
cout << "Date of birth is: August " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 9)
{
if (d1.day > 0 && d1.day <= 30)
{
cout << "Date of birth is: September " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 10)
{
if (d1.day > 0 && d1.day <= 31)
{
cout << "Date of birth is: October " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 11)
{
if (d1.day > 0 && d1.day <= 30)
{
cout << "Date of birth is: November " << d1.day << " " << d1.year << endl;
}
}
else if (d1.month == 12)
{
if (d1.day > 0 && d1.day <= 31)
{
cout << "Date of birth is: Desember " << d1.day << " " << d1.year << endl;
}
}
}
};
int main()
{
int day, month, year;
cout << "Enter the day of birth:";
cin >> day;
cout << "Enter the month of birth:";
cin >> month;
cout << "Enter the year of birth:";
cin >> year;
DOB d1(day,month,year);
invalidDay i1;
i1.check1(d1);
invalidMonth m1;
m1.check(d1);
getchar();
getchar();
return 0;
}
Comments
Post a Comment