Question

In: Computer Science

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

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


c++

Solutions

Expert Solution

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:


Related Solutions

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...
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
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...
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 -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT