I'm having trouble with understanding how classes are used with exceptions. Since I'm working with classes, do I still have to work with public/private and then use void?
I'm also confused about throw/catch. I believe I have the catch part right, but I don't know if the part throw part for invalidDay is right. I want the program to basically be able to detect that inputting 31 for a day in, let's say, January is invalid.
Assignment: 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: invalidDay and invalidMonth. If the user enters
an invalid value for day, then the program should throw and catch an
invalidDay object. Similar conventions for the invalid values of month
and year. (Note that your program must handle a leap year.).
I would appreciate it if someone would guide me to the correct direction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <string>
using namespace std;
int day;
int month;
int year;
void inputDate (int) throw (int day, invalidDay, invalidMonth);
int main()
{
try
{
cout << "\nEnter your birthday here in this format: mm/dd/yyyy--->: ";
cout <<"Enter day here.";
cin >> day;
cout <<"Enter the month here.";
cin >> month;
cout <<"Enter the year here.";
cin >> year;
if (day > 31)
throw invalidDay;
if(month==1)
cout << "January ";
else
if(month==2)
cout << "February ";
else
if(month==3)
cout << "March ";
else
if(month==4)
cout << "April ";
else
if(month==5)
cout << "May ";
else
if(month==6)
cout << "June ";
else
if(month==7)
cout << "July ";
else
if(month==8)
cout << "August ";
else
if(month==9)
cout << "September ";
else
if(month==10)
cout << "October ";
else
if(month==11)
cout << "November ";
else
if(month==12)
cout << "December ";
else
if (month > 12)
throw invalidMonth;
}
catch (invalidDay)
{
cout <<"The value that you entered is invalid, try again.";
}
catch (invalidMonth)
{
cout <<"The month you entered is invalid, try again.";
}
cout << "The date is " << month << " " << day << "," << year << "." << endl;
|