Question

In: Computer Science

Write a program that prompts the user to enter a person’s date of birth in numeric...

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.

Solutions

Expert Solution

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:)


Related Solutions

Write a program named CheckMonth2 that prompts a user to enter a birth month and day....
Write a program named CheckMonth2 that prompts a user to enter a birth month and day. Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day...
Write a program which prompts the user for the year of their birth, and which then...
Write a program which prompts the user for the year of their birth, and which then calculates their age (ignoring the month/day). Split the program into `main`, a "prompt for year" function which tests to make sure the user's input is a valid year, and a "calculate age" function which performs the actual calculation. (You can assume the current year is 2017.) for my intro to c++ class
Write a Java program that will add the digits of a person’s birth date to obtain...
Write a Java program that will add the digits of a person’s birth date to obtain a single digit to generate a numerology number. Write one separate method for each of the following tasks (it goes w/o saying that you will have a main() method along with these): date validating date crunching First: Get a Date Numerology has been used since ancient times to shed light on relationships, health, and global events. Each element in a birth date is believed...
Create a program that when run, prompts the user to enter a city name, a date,...
Create a program that when run, prompts the user to enter a city name, a date, the minimum temperature and the maximum temperature. Calculate the difference between the maximum temperature and the minimum temperature. Calculate the average temperature based on the minimum and maximum temperature. Print out the following: City name today's Date minimum temperature maximum temperature average temperature difference between minimum and maximum The following is a sample run, user input is shown in bold underline. Enter City Name:...
Develop a Java program that prompts the user to enter her birth year (e.g. 1980). The...
Develop a Java program that prompts the user to enter her birth year (e.g. 1980). The program then uses the current year (i.e. 2020) and displays age of the user (i.e. 40). The program should handle any exception that can occur due to the value entered by the user.
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
In this exercise, you’ll write a program that accepts a person’s birth date from the console...
In this exercise, you’ll write a program that accepts a person’s birth date from the console and displays the person’s age in years. To make that easier to do, we’ll give you a class that contains the code for accepting the birth date. The console output for the program should look something like this: f Welcome to the age calculator. j Enter the month you were born (1 to 12) : 5 Enter the day of the month you were...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT