In: Computer Science
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.)
Also the answer has to be written with 3 header files - main.cpp, invalidDay.h and invalidMonth.h
In the output, if we enter a wrong year or month; then the program should not show the output of "Invalid month" but it should again ask the user to re-enter the month or the thing that is entered incorrectly.
Hey There,
- I understood your question and I have given my answer that produces the exact results mentioned in the question.
- My code will also take care of Leap Year. I have provided all 3 files along with screenshots that show how my code performs with different test cases.
- I have also put comments so that you can understand what I did in the code.
- If you have any questions then do let me know in the comments box. I will be more than happy to help:)
1. invalidDay.h file
// Declaring invalidDay class
class invalidDay {
// Declaring public member functions
public:
void showException();
};
2. invalidMonth.h file
// Declaring invalidMonth class
class invalidMonth {
// Declaring public member functions
public:
void showException();
};
3. main.cpp file
// Including necessary header files
#include<bits/stdc++.h>
#include "invalidDay.h"
#include "invalidMonth.h"
using namespace std;
// Defining function of invalidDay class
void invalidDay::showException(){
cout << "The value of Day is invalid" << endl;
}
// Defining function of invalidMonth class
void invalidMonth::showException(){
cout << "The value of Month is invalid" << endl;
}
// Function to check if the year given by user is leap year or not
bool isLeapyear(int year){
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
return true;
else
return false;
}
else
return true;
}
return false;
}
// Function to check if the day is valid or not
bool isValidDay(int day, int month, int year){
// In some months there are 31 days so just checking the day as per the month value
if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day < 1 || day > 31))
return false;
if((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30))
return false;
// In leap year February month has 29 days so keeping this in mind
if((month == 2) && (isLeapyear(year) && (day < 1 || day > 29)))
return false;
if((month == 2) && (!isLeapyear(year) && (day < 1 || day > 28)))
return false;
// If the day is valid then returning true
return true;
}
// Function to check if the month entered by user is valid or not
bool isValidMonth(int month){
if(month < 1 || month > 12)
return false;
return true;
}
int main(){
// Declaring variables
string date;
string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
// Taking input from user
cout << "Enter Date of Birth in mm-dd-yyyy format: ";
getline(cin, date);
// We have date in the form of string so dividing the string in month, day and year
int month = stoi(date.substr(0, 2));
int day = stoi(date.substr(3, 5));
int year = stoi(date.substr(6, 10));
// Exception handling
try{
// checking if the day is valid or not
if(isValidDay(day, month, year) == false)
throw invalidDay();
// checking if the month is valid or not
if(isValidMonth(month) == false)
throw invalidMonth();
// If everything is valid then printing the date in the required format
cout << months[month-1] << " " << day << ", " << year << endl;
}
// If the day is not valid then raising an Exception
catch(invalidDay id){
id.showException();
// Asking user to re-enter the value of day
while(isValidDay(day, month, year) == false){
cout << "Please enter valid day value: ";
cin >> day;
}
// At last showing the date in the required format
cout << months[month-1] << " " << day << ", " << year << endl;
}
// If the month is not valid then raising an Exception
catch(invalidMonth im){
im.showException();
// Asking user to re-enter the value of month
while(isValidMonth(month) == false){
cout << "Please enter valid month value: ";
cin >> month;
}
// At last showing the date in the required format
cout << months[month-1] << " " << day << ", " << year << endl;
}
return 0;
}
Screenshot of code working on different solutions:
1. If User enters a valid date
2. If User enters invalid month
3. If User enters invalid day
4. LeapYear case
- If the given year is not a leap year then 29th February is not a valid day.
5. Leap Year Case - ||
- If the given year is a leap year then 29th February is a valid date (i.e. 2020 is a leap year)
Code for yyyy-mm-dd format:
// Including necessary header files
#include<bits/stdc++.h>
#include "invalidDay.h"
#include "invalidMonth.h"
using namespace std;
// Defining function of invalidDay class
void invalidDay::showException(){
cout << "The value of Day is invalid" << endl;
}
// Defining function of invalidMonth class
void invalidMonth::showException(){
cout << "The value of Month is invalid" << endl;
}
// Function to check if the year given by user is leap year or not
bool isLeapyear(int year){
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
return true;
else
return false;
}
else
return true;
}
return false;
}
// Function to check if the day is valid or not
bool isValidDay(int day, int month, int year){
// In some months there are 31 days so just checking the day as per the month value
if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day < 1 || day > 31))
return false;
if((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30))
return false;
// In leap year February month has 29 days so keeping this in mind
if((month == 2) && (isLeapyear(year) && (day < 1 || day > 29)))
return false;
if((month == 2) && (!isLeapyear(year) && (day < 1 || day > 28)))
return false;
// If the day is valid then returning true
return true;
}
// Function to check if the month entered by user is valid or not
bool isValidMonth(int month){
if(month < 1 || month > 12)
return false;
return true;
}
int main(){
// Declaring variables
string date;
string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
// Taking input from user
cout << "Enter Date of Birth in yyyy-mm-dd format: ";
getline(cin, date);
// We have date in the form of string so dividing the string in month, day and year
int month = stoi(date.substr(5, 7));
int day = stoi(date.substr(8, 10));
int year = stoi(date.substr(0, 4));
cout << day << " " << month << " " << year << endl;
// Exception handling
try{
// checking if the day is valid or not
if(isValidDay(day, month, year) == false)
throw invalidDay();
// checking if the month is valid or not
if(isValidMonth(month) == false)
throw invalidMonth();
// If everything is valid then printing the date in the required format
cout << months[month-1] << " " << day << ", " << year << endl;
}
// If the day is not valid then raising an Exception
catch(invalidDay id){
id.showException();
// Asking user to re-enter the value of day
while(isValidDay(day, month, year) == false){
cout << "Please enter valid day value: ";
cin >> day;
}
// At last showing the date in the required format
cout << months[month-1] << " " << day << ", " << year << endl;
}
// If the month is not valid then raising an Exception
catch(invalidMonth im){
im.showException();
// Asking user to re-enter the value of month
while(isValidMonth(month) == false){
cout << "Please enter valid month value: ";
cin >> month;
}
// At last showing the date in the required format
cout << months[month-1] << " " << day << ", " << year << endl;
}
return 0;
}
Proof of working:
Hope it helps:)