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

Write out a complete code with C++ language that will run the program. I\'d real

ID: 3626911 • Letter: W

Question

Write out a complete code with C++ language that will run the program. I'd really appreciate it.

You are to design and implement a program which validates dates expressed in the form of the string “mm-dd-yyyy”, e.g. “3-30-2012” and computes the day of the year to which they refer, e.g. the previous example specifies the 90th day of the year (31 days in January plus 29 days in February (2012 is a leap year) plus 30, the number of the day of March.)

A further note about leap years:
A year is a leap year if it is divisible by 4, but not by 100.
For example, 1998 and 2012 are leap years because they’re 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.
For example, 1600 and 2000 are divisible by 400 and are leap years.
However, 2200 is not a leap year because 2200 is not divisible by 400.

Your program should allow one or two digits for the month and day, i.e. it is not necessary to require January first be expressed as “01-01-2011”: “1-1-2011” suffices, but there would be nothing wrong with “01-01-2011” either.

Your program will read candidate strings for validation from the file “dates.txt” and output a line for each to the console. This output should echo the input and specify either the day of the year or give an explanation why it fails to meet the syntax criteria.

For example:

5-22-1951 is the 142 day of the year.
5-22-1952 is the 143 day of the year.
5-22-2000 is the 143 day of the year.
5-22-1900 is the 142 day of the year.
1-1 has an error: missing 2nd dash.
1-1-1- has an error: too many dashes.
2-30-2011 has an error: Invalid day.
13-1-1999 has an error: Invalid month.
1-0-1066 has an error: Invalid day.
-1-1 has an error: Dashes in wrong position.
mm-dd-yyyy has an error: Bad month digit.

The exact text of your error messages does not matter as long as the error is explained well and it does not matter which error is reported in the case of multiple errors (e.g. “0y-80” has an invalid month, a non-numeric character, a missing dash, an invalid day and is missing a dash and year.)


Your program must use functions to do its work. As always, there are a variety of ways to proceed. The following is one way to break the problem down into a manageable, testable set of functional components. You must implement and use these functions, following these prototypes.
NOTE: DO NOT write to the console in any of the following functions: they should silently return results up the chain to main where a determination can be made of how to use that result.

bool isLeapYear(int year);
/* Returns true if the specified year is a leap year; returns false otherwise. */

int getNumberOfDaysInMonth(int month, int year);
/* Returns the number of days in the specified month for the specified year. It is assumed these two values have already been validated as being a valid month (1 through 12) and year (non-negative). */

bool isGoodDayOfTheMonth(int month, int day, int year);
/* Returns true if the specified day is a valid day of the specified month in the specified year; returns false otherwise.*/

int checkValidDate(int month, int day, int year);
/* returns GOOD_DATE if the specified month, day and year specify a valid date; otherwise returns INVALID_MONTH if the month is invalid, INVALID_DAY if the day is invalid and INVALID_YEAR if the year is invalid.*/

int checkDashes(string date, size_t &firstDash, size_t &secondDash);
/* Determines whether the date string contains an appropriate set of dashes, e.g. passing “2-16-2011” in the date string would return GOOD_DATE.
The two size_t reference parameters are to be assigned the string position of the first and second dash, respectively.
Possible error returns are MISSING_DASH, MISSING_SECOND_DASH, TOO_MANY_DASHES and DASH_IN_WRONG_POSITION. */


int convertSubstringToNumber(string date, size_t firstPos, size_t secondPos);
/* Extracts the characters from the date string beginning at the location specified by firstPosition and continuing up through the locations specified by secondPosition to form an integer return value. It is assumed that the date string has already been validated, i.e. the postitions are valid and the substring’s characters are all numeric.*/

int checkDigits(string date, size_t firstDash, size_t secondDash);
/* Uses the two locations in the date string as the bounds to check for strictly numeric characters. All three date components, month, day and year, are checked for validity.
Returns GOOD_DATE if they are all numeric, otherwise returns BAD_MONTH_DIGIT, BAD_DAY_DIGIT, or BAD_YEAR_DIGIT as appropriate.*/

int extractDateComponents(string date, int &month, int &day, int &year);
/* Stores the month, day and year specified by the date parameter string into the three associated reference parameter integers.
This function should perform all of its work by calling functions already described. Functions checking for error conditions should be called in an appropriate order to avoid pasing unverified input to functions noted as assuming good input. Returns GOOD_DATE if successful, otherwise returns error values obtained from calling lower-level functions.*/

int computeDayOfYear(int month, int day, int year);
/* Given the specified month, day and year, computes the number of the day in the year. It is assumed the three input parameters specify a valid date.*/

int determineDayOfYear(string date);
/* Computes the day of the year for the specified date string. Returns that value if the date is valid; otherwise returns an error code indicating how the string was invalid. */

You must use the following error condition definitions:

#define GOOD_DATE 0
#define INVALID_MONTH -1
#define INVALID_DAY -2
#define INVALID_YEAR -3
#define MISSING_DASH -4
#define MISSING_SECOND_DASH -5
#define TOO_MANY_DASHES -6
#define DASH_IN_WRONG_POSITION -7
#define BAD_MONTH_DIGIT -8
#define BAD_DAY_DIGIT -9
#define BAD_YEAR_DIGIT -10
#define MISSING_DIGITS_BETWEEN_DASHES -11

It should be possible to implement these functions “from the bottom up,” in the approximate order described. A “test first” development approach is encouraged: write a series of calls to the function under development in the main() function, using various combinations of valid and invalid data as appropriate.

Compare the results of calling the function with what you would expect the proper result to be. When your expectations are not met, you should have a good idea of where in the function to look. Once all your expectations are met, you can go on to the next function, etc.

The more completely you test the lower level functions, the less prone you are to have to look at more than the function you are currently implementing, because whatever functions it calls should have already been demonstrated to work.

If you DO find a problem in a lower level function, add a test to its repertoire demonstrating the proper behavior and fix it before continuing to implement the function which called it and uncovered the untested failure.

A test file, dates.txt, has been supplied to help your testing and implementation. Think about how each of these inputs should be handled.

Remember: NO CONSOLE OUTPUT should be generated by the functions prototyped here!

Explanation / Answer

please rate - thanks

took hours but was fun. message me if any problems

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define GOOD_DATE 0
#define INVALID_MONTH -1
#define INVALID_DAY -2
#define INVALID_YEAR -3
#define MISSING_DASH -4
#define MISSING_SECOND_DASH -5
#define TOO_MANY_DASHES -6
#define DASH_IN_WRONG_POSITION -7
#define BAD_MONTH_DIGIT -8
#define BAD_DAY_DIGIT -9
#define BAD_YEAR_DIGIT -10
#define MISSING_DIGITS_BETWEEN_DASHES -11
bool isLeapYear(int year);
/* Returns true if the specified year is a leap year; returns false otherwise. */
int getNumberOfDaysInMonth(int month, int year);
/* Returns the number of days in the specified month for the specified year. It is assumed these two values have already been validated as being a valid month (1 through 12) and year (non-negative). */
bool isGoodDayOfTheMonth(int month, int day, int year);
/* Returns true if the specified day is a valid day of the specified month in the specified year; returns false otherwise.*/
int checkValidDate(int month, int day, int year);
/* returns GOOD_DATE if the specified month, day and year specify a valid date; otherwise returns INVALID_MONTH if the month is invalid, INVALID_DAY if the day is invalid and INVALID_YEAR if the year is invalid.*/
int checkDashes(string date, size_t &firstDash, size_t &secondDash);
/* Determines whether the date string contains an appropriate set of dashes, e.g. passing “2-16-2011” in the date string would return GOOD_DATE.
The two size_t reference parameters are to be assigned the string position of the first and second dash, respectively.
Possible error returns are MISSING_DASH, MISSING_SECOND_DASH, TOO_MANY_DASHES and DASH_IN_WRONG_POSITION. */
int convertSubstringToNumber(string date, size_t firstPos, size_t secondPos);
/* Extracts the characters from the date string beginning at the location specified by firstPosition and continuing up through the locations specified by secondPosition to form an integer return value. It is assumed that the date string has already been validated, i.e. the postitions are valid and the substring’s characters are all numeric.*/
int checkDigits(string date, size_t firstDash, size_t secondDash);
/* Uses the two locations in the date string as the bounds to check for strictly numeric characters. All three date components, month, day and year, are checked for validity.
Returns GOOD_DATE if they are all numeric, otherwise returns BAD_MONTH_DIGIT, BAD_DAY_DIGIT, or BAD_YEAR_DIGIT as appropriate.*/
int extractDateComponents(string date, int &month, int &day, int &year);
/* Stores the month, day and year specified by the date parameter string into the three associated reference parameter integers.
This function should perform all of its work by calling functions already described. Functions checking for error conditions should be called in an appropriate order to avoid pasing unverified input to functions noted as assuming good input. Returns GOOD_DATE if successful, otherwise returns error values obtained from calling lower-level functions.*/
int computeDayOfYear(int month, int day, int year);
/* Given the specified month, day and year, computes the number of the day in the year. It is assumed the three input parameters specify a valid date.*/
int determineDayOfYear(string date);
/* Computes the day of the year for the specified date string. Returns that value if the date is valid; otherwise returns an error code indicating how the string was invalid. */
int main()
{string date;
ifstream in;
int doy;
in.open("dates.txt");           //open file
   if(in.fail())             //is it ok?
       { cout<<"file did not open please check it ";
        system("pause");
        return 1;
        }

in>>date;
while( in)
     { doy=determineDayOfYear(date);   
      cout<<date<<" ";
      if(doy>0)
           cout<<"is the "<<doy<<" day of the year. ";
      else
         {cout<<"has an error: ";
         if(doy==-1)
               cout<<"invalid month. ";
          else if(doy==-2)
               cout<<"invalid day. ";
          else if(doy==-3)
               cout<<"invalid year. ";
          else if(doy==-4)
               cout<<"missing dash. ";     
          else if(doy==-5)
               cout<<"missing second dash. ";
           else if(doy==-6)
               cout<<"too many dashes. ";
           else if(doy==-7)
               cout<<"dash in wrong position. ";
            else if(doy==-8)
               cout<<"bad month digit. ";
           else if(doy==-9)
               cout<<"bad day digit. ";
           else if(doy==-10)
               cout<<"bad year digit. ";
           else
               cout<<"missing digits between dashes. ";
               }                      
      in>>date;
      }
     system("pause");
return 0;
}
int checkDashes(string date, size_t &firstDash, size_t &secondDash)
{int i,first=-1,second=-1;
for(i=0;i<date.length() ;i++)
     if(date[i]=='-')
          if(first<0)
               first=i;
          else
             if(second==-1)
                  second=i;
             else
                  return TOO_MANY_DASHES;
if(first==-1)
      return MISSING_DASH;
else if(second==-1)
      return MISSING_SECOND_DASH;
else if(first==second-1||first==0||second==i-1)
      return DASH_IN_WRONG_POSITION;
else
     {firstDash=first;
     secondDash=second;
      return GOOD_DATE;
     }
}
int convertSubstringToNumber(string date, size_t firstPos, size_t secondPos)
{int n=0,i;
for(i=firstPos;i<secondPos;i++)
       n=n*10+(date[i]-'0');
return n;
    }
int checkDigits(string date, size_t firstDash, size_t secondDash)
{int i;
   for(i=firstDash ;i<secondDash;i++)
        if(date[i]<'0'||date[i]>'9')
             return -1;

return GOOD_DATE;
    

}
int extractDateComponents(string date, int &month, int &day, int &year)
{int code;
size_t firstDash,secondDash;
code=checkDashes(date,firstDash,secondDash   );
if(code!=GOOD_DATE )
      return code;
month=checkDigits(date,0,firstDash   );
      if(month!=GOOD_DATE)
            return BAD_MONTH_DIGIT ;
      else
          month=convertSubstringToNumber(date,0,firstDash);
day=checkDigits(date,firstDash+1,secondDash-1   );
      if(day!=GOOD_DATE)
            return BAD_DAY_DIGIT ;
       else
          day=convertSubstringToNumber(date,firstDash+1,secondDash );    
year=checkDigits(date,secondDash+1,date.length() );
      if(year!=GOOD_DATE)
            return BAD_YEAR_DIGIT;
      else
          year=convertSubstringToNumber(date,secondDash+1,date.length());               
}
int determineDayOfYear(string date)
{int month,day,year,code;
code=extractDateComponents(date,month,day,year);
if(code<GOOD_DATE)
    return code;
code=checkValidDate(month,day,year);
if(code<GOOD_DATE)
    return code;
return computeDayOfYear(month,day,year);
}
bool isGoodDayOfTheMonth(int month, int day, int year)
{int days=getNumberOfDaysInMonth(month,year);
            if(day<1||day>days)
              if(month!=2)
                     return false;
            else
                  {if(day>29)
                       return false;
                if(!isLeapYear(year)&&day>28)
                        return false;
                  }
return true;     
}
int checkValidDate(int month, int day, int year)
{ if(month<1||month>12)
         return INVALID_MONTH ;
if(year<0||year>5000)           //didn't specify valid year range
         return INVALID_YEAR;
if(isGoodDayOfTheMonth(month,day,year))
         return GOOD_DATE;        
return INVALID_DAY ;       
          
}             
int computeDayOfYear(int month,int day,int year)      //change date to day of year
{int daysinmonth;
int days=0,i;
             
for (i=1;i<month;i++)       //go up to that month add how many days in the month
      days+=getNumberOfDaysInMonth(i,year);
days+=day;                           //add to the day wanted
return days;   
}
int getNumberOfDaysInMonth(int month, int year)
{int daysinmonth;
     switch(month)
         {
         case 9:
         case 4:
         case 6:
         case 11:daysinmonth=30;
                 break;
         case 2:if(isLeapYear(year))
                    daysinmonth=29 ;    //Feb has 28 or 29 days
               else
                   daysinmonth=28;
                 break;
         default:daysinmonth=31;
         }
    return daysinmonth;               //and count how many days passed
}

        
bool isLeapYear(int year)
{
    if(year%4==0)
    {if(year%100!=0)
            return true;
       else
           if(year%400==0)
               return true;
       }
return false;
}




Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote