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

Observe the following function synopsis. Function Synopsis: char *amonthLess(cha

ID: 3632010 • Letter: O

Question


Observe the following function synopsis.

Function Synopsis: char *amonthLess(char *dateString)
Description: Variable dataString is a valid date in a form of mm/dd/yyyy

Given the above information, write a main() program that asks a user to enter a date in a form of mm/dd/yyyy, and then call function amonthLess() that passes the date variable as an argument. The function will subtract one month from the entered date. For simplicity, assume that the date entered by the user is a valid date. However, you must not assume that the date format entered is always correct. You must check that the date format is strictly followed.

For example, if the date is 05/22/2008 then the function amonthLess() will return Apr 22, 2008. If the date is 01/23/2008 then the function returns Dec 23, 2007.


A typical execution of the above program looks like the following;

Enter a date in format mm/dd/yyyy (type q to quit): 3/23/2009
Invalid date format, try again.

Enter a date in format mm/dd/yyyy (type q to quit): 03/23/2009
One month less from the date entered is: Feb 23, 2009

Enter a date in format mm/dd/yyyy (type q to quit): 01/03/2010
One month less from the date entered is: Dec 3, 2009

Enter a date in format mm/dd/yyyy (type q to quit): 03/10/2010
One month less from the date entered is: Feb 10, 2010

Explanation / Answer

#include <iostream>

using namespace std;

int main()
{
   //PROTOTYPES
   char *aMonthLess(char *dateString);
   bool isValidDate(char *dateString);

   //LOCAL DECLARATIONS
   const int BUF_SIZE = 100;
   char buf[BUF_SIZE];

   //PROCEDURES
   cout << "Enter a date in format mm/dd/yyyy (type q to quit): ";
   cin.getline(buf, BUF_SIZE);
   while (toupper(buf[0]) != 'Q')
   {
      if (isValidDate(buf))
      {
         char *oneMonthLess = aMonthLess(buf);
         cout << "One month less from the date entered is: " << oneMonthLess << endl;
         delete [] oneMonthLess;
      }
      else
      {
         cout << "Invalid date format, try again. ";
      }
      cout << " Enter a date in format mm/dd/yyyy (type q to quit): ";
      cin.getline(buf, BUF_SIZE);
   }

   cout << endl;
   cin.get();
   return 0;
}

//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
char *aMonthLess(char *dateString)
{
   const char* monthShortName[12] = {
      "Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
   int month;
   int year;
   //int day;
   char *oneMonthLess = new char[13];

   //0123456789
   //mm/dd/yyyy
   month = (dateString[0] - '0') * 10 +
           (dateString[1] - '0');
   year = (dateString[6] - '0') * 1000 +
          (dateString[7] - '0') * 100 +
          (dateString[8] - '0') * 10 +
          (dateString[9] - '0');
   //day = (dateString[3] - '0') * 10 +
   //      (dateString[4] - '0');

   month--;
   if (month == 0)
   {
      month = 12;
      year--;
   }
   //if (month == 2)
   //{
   //   //if day > 28
   //   //check leap year or not, if not return Feb 28, else return Feb 29
   //}

   //0123456789ab
   //MMM dd, yyyy
   oneMonthLess[0] = monthShortName[month - 1][0];
   oneMonthLess[1] = monthShortName[month - 1][1];
   oneMonthLess[2] = monthShortName[month - 1][2];
   oneMonthLess[3] = ' ';
   oneMonthLess[4] = dateString[3]; //short way without calculate day
   oneMonthLess[5] = dateString[4]; //if day need to change for Feb then
   //oneMonthLess[4] = (day / 10) + '0';
   //oneMonthLess[5] = (day % 10) + '0';
   oneMonthLess[6] = ',';
   oneMonthLess[7] = ' ';
   oneMonthLess[8] = (year / 1000) + '0';
   year %= 1000;
   oneMonthLess[9] = (year / 100) + '0';
   year %= 100;
   oneMonthLess[10] = (year / 10) + '0';
   oneMonthLess[11] = (year % 10) + '0';
   oneMonthLess[12] = '';

   return oneMonthLess;
}
//---------------------------------------------------------
//---------------------------------------------------------
bool isValidDate(char *dateString)
{
   bool isNumChar(char chr);
   int size = 0;

   for (char *ptr = dateString; *ptr != ''; ++ptr)
   {
      size++;
   }

   //0123456789
   //mm/dd/yyyy
   if (size != 10)
   {
      return false;
   }
   else if (!isNumChar(dateString[0]) ||
            !isNumChar(dateString[1]) ||
            !isNumChar(dateString[3]) ||
            !isNumChar(dateString[4]) ||
            !isNumChar(dateString[6]) ||
            !isNumChar(dateString[7]) ||
            !isNumChar(dateString[8]) ||
            !isNumChar(dateString[9]) )
   {
      return false;
   }
   else if (dateString[2] != '/' || dateString[5] != '/')
   {
      return false;
   }
   else
   {
      return true;
   }
}
//---------------------------------------------------------
//---------------------------------------------------------
bool isNumChar(char chr)
{
   return (chr >= '0' && chr <= '9');
}