In: Computer Science
Write 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.)
In case of any query, do comment. Please rate answer. Thanks
Please use below code:
#include<iostream>
#include<ctime>
#include <exception>
#include <string.h>
using namespace std;
int ValidateDate(int day, int month,int year);
//Exception classes
struct InvalidDay : public std::exception
{
const char * what () const throw ()
{
return "Invalid Day Exception!!";
}
};
struct InvalidMonth : public std::exception
{
const char * what () const throw ()
{
return "Invalid Month Exception!!";
}
};
struct InvalidYear : public std::exception
{
const char * what () const throw ()
{
return "Invalid Year Exception!!";
}
};
int main()
{
string inputTime,originalInputTime;
int day, month,year;
cin >> originalInputTime; // To read the string
inputTime = originalInputTime; //preserve the original string
string delimiter = "-";
int positionOfToken = inputTime.find(delimiter);
//extract month from the string, based on delimiter "-"
month = stoi(inputTime.substr(0,positionOfToken));
inputTime = inputTime.erase(0,positionOfToken+1); //erase the string
//extract day
positionOfToken = inputTime.find(delimiter);
day = stoi(inputTime.substr(0,positionOfToken));
inputTime = inputTime.erase(0,positionOfToken+1); //erase the string
//extract year
positionOfToken = inputTime.find(delimiter);
year = stoi(inputTime.substr(0,positionOfToken));
try {
//try to validate the date, if exception thrown then catch it if successful then add print the desired output
if(ValidateDate(day,month,year) == 0)
{
char date_string[100];
struct tm dateOfBirth = {}; //declare empty time struct, construct the time struct using originalInputTime
if(strptime(originalInputTime.c_str(), "%m-%d-%Y", &dateOfBirth) == NULL) //convert the given string into the date format separated by -
{
std::cout << "parsing failed" << std::endl;
}
//convert the date in desired format
strftime(date_string, 50, "%B %d, %Y", &dateOfBirth);
//print the date
cout << date_string;
}
}
//exception for InvalidDay, InvalidMonth, InvalidYear
catch(InvalidDay& e) {
std::cout << e.what() << std::endl;
}
catch(InvalidMonth& e) {
std::cout << e.what() << std::endl;
}
catch(InvalidYear& e) {
std::cout << e.what() << std::endl;
}
catch(std::exception& e) {
//other standard errors
std::cout << e.what() << std::endl;
}
return 0;
}
//function to validate date
int ValidateDate( int day, int month, int year )
{
if( year < 0 )
throw InvalidYear();
if( month < 0 || month > 12)
throw InvalidMonth();
if(day < 0 )
throw InvalidDay();
//validation for 30 days
if( month == 4 || month == 6 || month == 9 || month == 11)
{
if( day > 30)
throw InvalidDay();
}
else if( month == 2) //leap year validation
{
if ( year % 4 == 0 )
{
if( day > 29 )
throw InvalidDay();
}
else
{
if( day > 28)
throw InvalidDay();
}
}
else
{
//else wold be Months having 31 days
if( day > 31)
throw InvalidDay();
}
return 0; //successful validation
}
===============screen shot============
output: