Exercise 1 50 points Contracts and other legal documents are

Exercise 1 (50 points) Contracts and other legal documents are often dated in the following way Dated thisday of The first blank is replaced by the day of the month, like 1st, 2nd, 3rd, 10th, etc. The second blank is the name of the month, January, February, etc. and the third blank has the year. Write a program that displays dates in this form. Ask the user to enter a date in mm/dd/yyyy form and display the corresponding date in legal form. Use at least one switch statement in your program. Include appropriate input validation: don\'t accept years less than one, April, June, September, and November have only thirty days, valid months are 1 through 12 inclusive. On leap years February has twenty-nine days. On non-leap years February has twenty-eight days. Leap years occur in years that are evenly divisible by 4 unless the year is evenly divisible by 100 and not by 400. It is best to submit a program that produces the correct outputs for most days (i.e. without some or all of the input validation) than to submit a program that does not compile. If some tests produce correct results you will get that portion of the credit. Sample output from a program that satisfies the requirements of this exercise is given below. Sample Output 1 Enter the date (mm/dd/yyyy): 04/11/1994 Dated this llth day of April, 1994 Sample Output 2 Enter the date (mm/dd/yyyy): 06/30/1959 Dated this 30th day of June, 1959

Solution

Please follow the code and comments for description :

CODE :

#include <iostream>
#include <cstring>
#include <fstream>
#include <sstream>

using namespace std;

int main() { // driver method with the throws expected clauses
  
   int l, y, d, m; // required initialisations
   string dd, mm, yy;

   int maximumDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //array of dates that stores the maximum days of every month

   string monthsNames[] = {\"\", \"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\",
       \"September\", \"October\", \"November\", \"December\"}; //array storing the month names

   cout << \"Enter any Date in (mm/dd/yyyy) format : \"; // prompt to enter the date to the user
   string inDate = \" \"; //getting the date in String format
   cin >> inDate;

   l = inDate.length(); //finding the number of digits in the given input

   if (l == 10) //performing the task only when number of digits is 10
   {
       mm = inDate.substr(0, 2); //getting the day in String format
       dd = inDate.substr(3, 5); //getting the monthsNames in String format
       yy = inDate.substr(6); //getting the year in String format
       d = atoi(dd.c_str()); //converting the day to Integer format
       m = atoi(mm.c_str()); //converting the month to Integer format
       y = atoi(yy.c_str()); //converting the year to Integer format

       if ((y % 400 == 0) || ((y % 100 != 0) && (y % 4 == 0))) // checking the condition for leap year
       {
           maximumDays[2] = 29;
       }

       if (m < 0 || m > 12) { // Performing the Date Validation
           cout << \"Invalid Date Entered.\" << endl;
           cout << \"Month must be in between 1 and 12 Inclusive.\" << endl;
       } else if (d < 0 || d > maximumDays[m]) {
           cout << \"Invalid Date Entered.\" << endl;
           cout << \"Day is Not Valid.\" << endl;
       } else if (y < 0 || y > 9999) {
           cout << \"Invalid Date Entered.\" << endl;
           cout << \"Year is not Valid.\" << endl;
       } else {
           if ((dd == \"01\") || (dd == \"1\") || (dd == \"21\") || (dd == \"31\")) { // checking for the date to get the formatted output based on the day
               cout << \"Dated this \" << dd << \"st day of \" << monthsNames[m] << \", \" << yy << \".\";
           } else if ((dd == \"02\") || (dd == \"2\") || (dd == \"22\")) {
               cout << \"Dated this \" << dd << \"nd day of \" << monthsNames[m] << \", \" << yy << \".\";
           } else if ((dd == \"03\") || (dd == \"3\") || (dd == \"23\")) {
               cout << \"Dated this \" << dd << \"rd day of \" << monthsNames[m] << \", \" << yy << \".\";
           } else {
               cout << \"Dated this \" << dd << \"th day of \" << monthsNames[m] << \", \" << yy << \".\";
           }
       }
       cout << endl;
   } else {
       cout << \"Wrong Input.\";
   }
}

OUTPUT :

1)

Enter any Date in (mm/dd/yyyy) format : 06/30/1959
Dated this 30th day of June, 1959.

2)

Enter any Date in (mm/dd/yyyy) format : 09/31/2016
Invalid Date Entered.
Day is Not Valid.

3)

Enter any Date in (mm/dd/yyyy) format : 13/07/1994
Invalid Date Entered.
Month must be in between 1 and 12 Inclusive.


Hope this is helpful.

 Exercise 1 (50 points) Contracts and other legal documents are often dated in the following way Dated thisday of The first blank is replaced by the day of the
 Exercise 1 (50 points) Contracts and other legal documents are often dated in the following way Dated thisday of The first blank is replaced by the day of the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site