Write a program that prompts the user to enter a person\'s date of birth in numb
ID: 3654707 • Letter: W
Question
Write a program that prompts the user to enter a person's date of birth in numberi form such as 8-27Write 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 the 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.)Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class invalidDay
{
string msg;
public:
invalidDay()
{
msg="Day input is wrong";
}
void showException()
{
cout<<msg<<endl;
}
};
class invalidMonth
{
string msg;
public:
invalidMonth()
{
msg="Month input is wrong";
}
void showException()
{
cout<<msg<<endl;
}
};
class leapYear
{
string msg;
public:
leapYear()
{
msg="year input is wrong";
}
void showException()
{
cout<<msg<<endl;
}
};
void read_date(int &day,int &month,int &year);
int main()
{
int day,month,year;
string months[12]={"january","feburary","march",
"april","may","june","july","august","september",
"octomber","november","december"};
try
{
read_date(day,month,year);
cout<<"Date of Birth "<<months[month-1]<<" "<<day<<","<<year;
}
catch(invalidDay id)
{
id.showException();
}
catch(invalidMonth im)
{
im.showException();
}
catch(leapYear ly)
{
ly.showException();
}
system("pause");
return 0;
}
void read_date(int &d,int &m,int &y)
{
cout<<"Enter day"<<endl;
cin>>d;
if(d<=0 ||d>31)
throw invalidDay();
cout<<"Enter month"<<endl;
cin>>m;
if(m<=0 ||m>=13)
throw invalidMonth();
cout<<"Enter year"<<endl;
cin>>y;
if(y%400==0||(y!=100&&y%4==0))
if(d>=30)
throw leapYear();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.