So i believe i got my prefix working, now i'm trying to get my postfix working so that the class object happy should have its day increased by 1. Not sure whats wrong. Maybe the way it receives the month and day in the constructor?

main

#include <iostream>
#include <string>

#include "DayOfYear.h"
using namespace std;

void print(int);
int main()
{
    DayOfYear dYear, happy;
    int day;
    string month;

    do{
    cout << "\nEnter the day number and Enter 911 to stop the program";
    cout << endl << "Day Entered: ";
    cin >> day;
    dYear.setDay(day);
    if(day==911){
        cout << "The Program has Ended" << endl;
    }

    //++dYear;
    //dYear.print(day);

    //Demonstrating overloaded prefix ++ operator
    cout << "Demonstrating overlaoded prefix ++ operator.\n";

    ++happy;
    cout<< happy.getDay();
    happy.print(day);

    //Demonstrating overloaded postfix ++ operator
    cout << "\nDemonstrating overloaded postfix ++ operator.\n";
    happy++;
    cout<< happy.getDay();
    happy.print(day);

    }while(day!=911);


    return 0;

}
//Send entered day number to the print() function
void DayOfYear::print(int day)
{
    int index = 0;

    while (DayOfYear::MonthDay[index] < day)
        index = (index + 1) %12;

        //Display month and day
        cout << DayOfYear::Month[index] << " " << day - DayOfYear::MonthDay[index-1];
};

DayOfYear.cpp

#include "DayOfYear.h"
#include <cmath>
#include <iostream>
using namespace std;

//Set days of each month into an array
const int DayOfYear::MonthDay[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

//Set the name of each month into an array
const string DayOfYear::Month[] = {"January", "February", "March",
                                    "April", "May", "June",
                                    "July", "August", "September",
                                    "October", "November", "December"};

//************************************************
// Overloaded prefix ++ operator. Causes the     *
// inched member to be incremented. Returns the  *
// incremented  object.                          *
//************************************************
DayOfYear DayOfYear::operator++()
{
    ++day;
    simplify();
    return *this;
}


//*************************************************
// Overloaded postfix ++ operator. Causes the     *
// inches member to be incremented. Returns the   *
// value of the object before the increment.      *
//*************************************************
DayOfYear DayOfYear::operator++(int)
{
    DayOfYear temp(months, day);
    day++;
    simplify();
    //cout<<day;
    return temp;
    }

//*******************************************************
// Overloaded postfix -- operator.                    *
//*******************************************************
DayOfYear DayOfYear::operator--(int)
{
    string month;
    DayOfYear temp(month, day);
    day--;
    if (day == -1) // <-------------- fix to ... if (day ==-1)
    { day = day + 365; }
    return temp;
}
void DayOfYear::simplify()
{
    string month;

    //Incrementors
    for(int index=0;index<12;index++){
    month=DayOfYear::Month[index];
    if (month== "April" ||month== "June" || month=="September" ||month== "November" && day > 30)
    {
        month += (day / 30);
        day = (day % 30);
    }
    else if ( month =="January"||month=="March"||month=="May" ||month=="July" ||month=="August" ||month=="October" && day > 31)
    {
        month += (day / 31);
        day = day % 31;
    }
    else if ( month == "Feburary" && day>28 )
    {
        month += (day / 28);
        day = day % 28;
    }

    else if (month == "December" && day > 31)
    {
        month = (day / 31);
        day = day % 31;
    }
    }
}
/*
    //Decrementors
    else if (month== "April" ||month== "June" || month=="September" ||month== "November" && day < 1)
    {
        month -= ((abs(day) / 30) + 1);
        day = day - (abs(day) % 30);
    }
    else if (month=="March"||month=="May" ||month=="July" ||month=="August" ||month=="October"||month == "December" && day < 1)
    {
        month -= ((abs(day) / 31) +1);
        day = 31 - (abs(day) % 31);
    }
    else if (month == "Feburary" && day < 1)
    {
        month -= ((abs(day) / 28) +1);
        day = 28 - (abs(day) % 28);
    }
    else if (month =="January" && day < 1)
    {
        month -= ((abs(day) / 31) + 1);
        day = 31 - (abs(day) % 31);
    }

 }
}
*/

DayOfYear.h

#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H

#include <string>
using namespace std;

class DayOfYear
{
    private:
        int day;
        string months;
        static const string Month[12];                            //static member variables
        static const int MonthDay[12];


    public:
    void print(int);
    void simplify();
    void setDay(int d){day = d;}
    int getDay(){return day;}


        DayOfYear()
        {
            day = 0;
        }
        DayOfYear(int d)
        {
            day = d;
        }

    DayOfYear (string m,int d)
    {
        months=m;
        day= d;
        simplify();
    }


    // Overloaded operator function.
    DayOfYear operator++();
    DayOfYear operator++(int);
    DayOfYear operator--(int);
};
#endif 

Not wanting to go through all this code, I have one suggestion - use julian date values. There are standard expressions to take a julian date (either unsigned integer for y/m/d, or double float for y/m/d/h:m:s) and extract the day of the week, accomodating leap years, and even leap-seconds (if necessary).

Also this:

    if(day==911){
        cout << "The Program has Ended" << endl;
    }

should be this:

    if(day==911){
        cout << "The Program has Ended" << endl;
        return 0;
    }

And FWIW, I have written numerous date processing functions over the years, in language ranging from BASIC to dBase II to C++ and Java date/time classes. They all properly dealt with leap years/seconds, days of week, etc. So, first convert the date to a Julian value. Then, the rest is simple math.

I just want to keep it simple with just the month and day because there were no instructions to do the year or the leap year. The only problem i have is how to print the information from the postfix operator using the print function or something else.

Here's some code to look at. I changed things around a little. I used months to hold the month and day string for displaying. I used simplify() to set it, using a stringstream. I changed the setDay function to accommodate days greater than 365 and less than 1, so that it automatically rolls over and sets the months string with the month and day value for that day. I call that in the operators instead of having a different routine to do the same thing. Not sure if it's all 100% what you want, but it all works including the operators.

main.cpp

    #include <iostream>
    #include <string>
    #include "DayOfYear.h"
    using namespace std;
    void print(int);
    int main()
    {
        DayOfYear dYear, happy;
        int day;
        string month;
        do
        {
            cout << "\nEnter the day number and Enter 911 to stop the program";
            cout << endl << "Day Entered: ";
            cin >> day;
            dYear.setDay(day);
            dYear.print();
            cout << endl;
            if(day==911)
            {
                cout << "The Program has Ended" << endl;
            }
            //Demonstrating overloaded prefix ++ operator
            cout << "Demonstrating overlaoded prefix ++ operator.\n";
            happy.setDay(day);
            ++happy;
            happy.print();
            cout << endl;
            //Demonstrating overloaded postfix ++ operator
            cout << "\nDemonstrating overloaded postfix ++ operator.\n";
            happy++;
            happy.print();
            cout << endl;
            //Demonstrating overloaded prefix -- operator
            cout << "Demonstrating overlaoded prefix -- operator.\n";
            --happy;
            happy.print();
            cout << endl;
            //Demonstrating overloaded postfix -- operator
            cout << "\nDemonstrating overloaded postfix -- operator.\n";
            happy--;
            happy.print();
            cout << endl;
        }
        while(day!=911);
        return 0;
    }
    //Send entered day number to the print() function
    void DayOfYear::print()
    {
        //Display month and day
        cout << months;
    };

DayOfYear.cpp

#include "DayOfYear.h"
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;
//Set days of each month into an array
const int DayOfYear::MonthDay[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//Set the name of each month into an array
const string DayOfYear::Month[] = {"January", "February", "March",
                                   "April", "May", "June",
                                   "July", "August", "September",
                                   "October", "November", "December"
                                  };
//************************************************
// Overloaded prefix ++ operator. Causes the *
// inched member to be incremented. Returns the *
// incremented object. *
//************************************************
DayOfYear DayOfYear::operator++()
{
    setDay(day + 1);
    simplify();
    return *this;
}
//*************************************************
// Overloaded postfix ++ operator. Causes the *
// inches member to be incremented. Returns the *
// value of the object before the increment. *
//*************************************************
DayOfYear DayOfYear::operator++(int)
{    
    DayOfYear temp(*this);
    operator ++();  
    return temp;
}
//*******************************************************
// Overloaded prefix -- operator. *
//*******************************************************
DayOfYear DayOfYear::operator--()
{
    setDay(day - 1);
    simplify(); 
    return *this;
}
//*******************************************************
// Overloaded postfix -- operator. *
//*******************************************************
DayOfYear DayOfYear::operator--(int)
{
    DayOfYear temp(*this);
    operator --();  
    return temp;
}
void DayOfYear::simplify()
{
    stringstream month;
    //Incrementors
    for(int index=0; index<12; index++)
    {
        if(MonthDay[index] >= day)
        {
            if(index == 0)
                month << Month[index] + " " << day;
            else
                month << Month[index] + " " << day - MonthDay[index-1];
            months = month.str();
            break;
        }
    }
}

DayOfYear.h

   #ifndef DAYOFYEAR_H
    #define DAYOFYEAR_H
    #include <string>
    using namespace std;
    class DayOfYear
    {
    private:
    int day;
    string months;
    static const string Month[12]; //static member variables
    static const int MonthDay[12];
    public:
    void print();
    void simplify();
    void setDay(int d)
    {
        day = d;
        if(day > 365)
            day = day % 365;
        if(day < 1)
            day = day % 365 + 366;
        simplify();
    }
    int getDay(){return day;}
    DayOfYear()
    {
    day = 0;
    }
    DayOfYear(int d)
    {
    day = d;
    simplify();
    }
    // Overloaded operator function.
    DayOfYear operator++();
    DayOfYear operator++(int);
    DayOfYear operator--();
    DayOfYear operator--(int);
    };
    #endif
commented: Thanks so much for the example. I didn't really understand how operators work but from you example it explained alot. Thanks again. +0

You're very welcome If your question is answered, please remember to mark this solved thanks.

I just wanted to make sure the program worked before i mark the post.I was trying to do the method by myself with the guidance of your example and it seems to work perfectly, although I didn't make the change in the print and simplify function.(hopefully there isnt anything wrong) I did used the if statement for the setDay since thats needed for the --operator. Thanks again for the help.

Actually had to changed the simplify function so that it gets the right month, and its way easier to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.