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. Follow similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)
#include<iostream>
using namespace std;
class invalidDay{
private:
string message;
public:
invalidDay(){
message = "Invalid day";
}
void showError(){
cout<<message<<endl;
}
};
class invalidMonth{
public:
string message;
invalidMonth(){
message = "Invalid month";
}
void showError(){
cout<<message<<endl;
}
};
class LeapYear{
public:
bool checkYear(int year)
{
return (((year % 4 == 0) && (year % 100 != 0)) ||
(year % 400 == 0));
}
void showError(int year){
if (checkYear( year) == 1){
}
else{
cout<<"Invalid days\n";
}
}
};
int inputData(int day, int month, int year){
LeapYear le;
invalidDay d;
int t;
if(day>31){
invalidDay d;
d.showError();
t = 0;
}
else if(month>12){
invalidMonth m;
m.showError();
t=0;
}
else if(month==2 && le.checkYear(year)==0 && day != 28){
d.showError();
t=0;
}
else if(month==2 && le.checkYear(year)==1 && day != 29){
d.showError();
t =0;
}
else{
t=1;
}
return t;
}
int main(){
string months[12]={"January","February","March","April","May","June","July","August","September", "October","November","December"};
int day; int month, year;
do{
cout<<"Enter day:\n";
cin>>day;
cout<<"Enter month:\n";
cin>>month;
cout<<"Enter year:\n";
cin>>year;
}while(inputData(day, month, year)==0);
cout<<"Date of Birth: "<<months[month-1]<<" "<<day<<","<<year;
}
Comments
Leave a comment