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 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
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:...
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:...
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 Python program that asks the user to enter a student's name and 8 numeric...
Write a Python program that asks the user to enter a student's name and 8 numeric tests scores (out of 100 for each test). The name will be a local variable. The program should display a letter grade for each score, and the average test score, along with the student's name. Write the following functions in the program: calc_average - this function should accept 8 test scores as arguments and return the average of the scores per student determine_grade -...
PYTHON Write a program that asks the user to enter a student's name and 8 numeric...
PYTHON Write a program that asks the user to enter a student's name and 8 numeric assignment scores (out of 100 for each assignment). The program should output the student's name, a letter grade for each assignment score, and a cumulative average for all the assignments. Please note, there are 12 students in the class so your program will need to be able to either accept data for 12 students or loop 12 times in order to process all the...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT