Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Typically, calendar dates are read and printed in an all-numeric form consisting

ID: 3866485 • Letter: T

Question

Typically, calendar dates are read and printed in an all-numeric form consisting of the following data elements from left to right: MM-DD-YYYY. Some applications, however, might require the date to be printed in another form. Write a program that asks the user to input a date in an all numeric form such as 07-31-2017, then outputs the date in the form: July 31, 2017. You should define three exception classes, each in a separate header file, to catch unexpected behavior in your program: incorrectYear, incorrectMonth and incorrectDay. If the user inputs an incorrect value for year, or month or day, then an exception should be thrown and an incorrectYear, or incorrectMonth or incorrectDay object should be catched. The year should be nonnegative and greater than or equal to 1900. Also, your program should check for a leap year. Recall that a year is a leap if it is divisible by 4, but not by 100 (e.g., 1996 and 2004 are divisible by 4, but not by 100). A year that is divisible by 100 is a leap year if it is also divisible by 400 (e.g., 1600 and 2000 are divisible by 400). Nonetheless, 1900 is not a leap year because it is not divisible by 400.

Explanation / Answer

#include<iostream>
#include <cstdio>
#include <string>
using namespace std;


int day, month, year;

void acceptDate() {
   day = month = year = -1;
   string dateString;
   cout << "Enter date in format MM-DD-YYYY: ";
   cin >> dateString;
   sscanf(dateString.c_str(), "%d-%d-%d", &day, &month, &year);
   if (year < 1900)
       throw "incorrectYear";
   if(month < 1 || month > 12)
       throw "incorrectMonth";
   int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
   if (month == 2 && ((year % 4==0 && year % 100!=0) || year%400 == 0))
       days[2] = 29;
   if (day < 0 || day > days[month])
       throw "incorrectDay";

   cout << "Date accecped successfully";
}

int main() {
   try {
       acceptDate();
   } catch (string e) {
       cout << "Exception: " << e;
   }

}

Buddy let me know if you are getting runtime error! :(