// File:  ex3-5.cpp - The Date class

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

const unsigned DaysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};

class Date
{
private:
    unsigned day;
    unsigned month;
    unsigned year;
    void errmsg(const char* msg) const;
public:
    void set(const char* mmddyy);
    void increment();
    void display() const;
};

void Date::set(const char* mm_dd_yy)
{
    char* temp;
    char copy[9];

    // assume user enters date as mm/dd/yy
    if (strlen(mm_dd_yy) != 8) errmsg(mm_dd_yy);

    // use a copy of mm_dd_yy        What is the impact to the function?
    strcpy(copy,mm_dd_yy);

    // parse the date and get the month
    temp = strtok(copy,"/");    // strtok() replaces the "/" with a NULL
    if (temp != NULL) month = atoi(temp);    // atoi() converts a string to an int
    else errmsg(copy);

    // parse the date and get the day
    temp = strtok(NULL,"/");    // strtok() finds the next "/"
    if (temp != NULL) day = atoi(temp);
    else errmsg(copy);

    // parse the date and get the year
    temp = strtok(NULL,"/");
    if (temp != NULL) year = atoi(temp);
    else errmsg(copy);

    // Make a Y2K correction for a 2-digit year
    if (year < 50) year += 2000;
    else if (year < 100) year += 1900;
    else ;    // assume the year is right
}

void Date::increment()
{
    // increment the day
    day++;

    // check for the end of the month
    if (day > DaysPerMonth[month - 1])    // gone past end of current month?
    {
        month ++;
        day = 1;
    }

    // check for the end of the year
    if (month > 12)
    {
        year ++;
        month = 1;
    }
}

void Date::display() const
{
    cout << "The date is " << month << '/' << day << '/'
        << (year%100< 10?"0":"") << year%100 << endl;
    if (day % DaysPerMonth[month-1] == 0) cout << endl;
}

void Date::errmsg(const char* msg) const
{
    cerr << "Invalid date format: " << msg << endl;
    exit(EXIT_FAILURE);
}

int main()
{
    Date d;
    char mmddyy[9];
    cout << "Enter the starting date <mm/dd/yy> => ";
    cin >> mmddyy;
    d.set(mmddyy);
    for (int i = 0; i < 375; i++)
    {
        d.display();
        d.increment();
    }
}