Issues Overloading Operators

I have been struggling with a problem that asks me to convert a day of the year (1-365) to the corresponding month and day of that month (ie 1 = January 1, 365 = December 31, etc.) I've also been asked to overload the prefix and postfix increment and decrement operators but I'm not sure I'm quite there yet.

At the moment I'm just trying to get my program to take a number from the user as input, convert that number to a month and day, and then use my overloaded increment and decrement to add a day and print out the next month and day, then subtract a day and print that out as well. I can't seem to get my overloaded operators to work at all..

I could really use some advice or guidance. Any help would be much appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Here is my code so far:

#include<iostream>
#include<string>
#include <cstdlib>

using namespace std;

class DayOfYear
{
    private:

        static string month;

    public:
        int day;
        DayOfYear();
        DayOfYear(int d);
        DayOfYear(string m, int d);
        void print();
        DayOfYear operator++(int);
        DayOfYear operator--(int);

};

string DayOfYear::month;

DayOfYear DayOfYear::operator++(int) {
    DayOfYear temp = *this;

    if (day == 365) {
        day = 1;
    } else {
        day++;
    }
    print();
    return temp;

}


DayOfYear DayOfYear::operator--(int) {
    DayOfYear temp = *this;

    if (day == 1) {
        day = 365;
    } else {
        day--;
    }
    print();
    return temp;


}

DayOfYear::DayOfYear(string m, int d) {
    month = m;
    day = d;

    if (m == "January" && d > 31) {
        cout << "Invalid input, terminating program." << endl;
        //system("PAUSE");
        exit(EXIT_FAILURE);
    } else if (m == "February" && d > 28) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "March" && d > 31) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "April" && d > 30) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "May" && d > 31) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "June" && d > 30) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "July" && d > 31) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "August" && d > 31) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "September" && d > 30) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "October" && d > 31) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "November" && d > 30) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } else if (m == "December" && d > 31) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    }

    cout << month << " " << day << endl;
}

DayOfYear::DayOfYear()
{
    cout << "Enter a day number." << endl;
    cin >> day;
    print();


}

DayOfYear::DayOfYear(int d)
{
    day = d;

    while (d > 365 || d < 1) {
        cout << "Day cannot be > 365 or < 1. " << endl;
        cin >> d;
        day = d;
    }

    print();
}

void DayOfYear::print() {
    if (day > 0 && day < 32)
    {
        month = "January";
        cout << month << " " << 31 + day - 31 << endl;
    }
    else if (day > 31 && day < 60)
    {
        month = "February";
        cout << month << " " << day - 31  << endl;
    }
    else if (day > 59 && day < 91)
    {
        month = "March";
        cout << month << " " << day - 59 << endl;
    }
    else if (day > 90 && day < 121)
    {
        month = "April";
        cout << month << " " << day - 90 << endl;
    }
    else if (day > 120 && day < 152)
    {
        month = "May";
        cout << month << " " << day - 120 << endl;
    }
    else if (day > 151 && day < 182)
    {
        month = "June";
        cout << month << " " << day - 151 << endl;
    }
    else if (day > 181 && day < 213)
    {
        month = "July";
        cout << month << " " << day - 181 << endl;
    }
    else if (day > 212 && day < 244)
    {
        month = "August";
        cout << month << " " << day - 212 << endl;
    }
    else if (day > 243 && day < 274)
    {
        month = "September";
        cout << month << " " << day - 243 << endl;
    }
    else if (day > 273 && day < 305)
    {
        month = "October";
        cout << month << " " << day - 273 << endl;
    }
    else if (day > 304 && day < 335)
    {
        month = "November";
        cout << month << " " << day - 304  << endl;
    }
    else if (day > 334 && day < 366)
    {
        month = "December";
        cout << month << " " << day - 334 << endl;
    }
    exit(0);
}


int main()
{
    DayOfYear day;

    cout << day.day << endl;
    cout << (day++).day << endl;
    cout << (day--).day << endl;

    system("pause");
    return 0;
}


Last edited on
Here you go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
DayOfYear& DayOfYear::operator++(){
	if (this->day == 365)
		this->day = 1;
	else
		this->day++;
	return *this;
}
DayOfYear DayOfYear::operator++(int){
	DayOfYear tmp(*this);
	operator++();
	return tmp;
}
DayOfYear& DayOfYear::operator--(){
	if (this->day == 1)
		this->day = 365;
	else
		this->day--;
	return *this;
}
DayOfYear DayOfYear::operator--(int){
	DayOfYear tmp(*this);
	operator--();
	return tmp;
}


What about leap years?
Last edited on
Thanks!

Yes, leap years, but I'm still trying to wrap my head around this...
I am getting a compiler error for my prototypes. How can I write prototypes for the functions that are returning pointers?

Something like:

DayOfYear& operator++();
DayOfYear& operator--(int);
Last edited on
1
2
3
4
DayOfYear& operator++();
DayOfYear operator++(int);
DayOfYear& operator--();
DayOfYear operator--(int);
Thank you mobutus, you've been a big help.

Still having the same issue as a mentioned above trying to get the day to increment and decrement properly.

1
2
3
4
5
6
7
8
    cout << "Enter a day number." << endl;
    cin >> day;

    print();
    day++;
    print();
    day--;
    print();


My issue is with the decrement. It is decrementing the incremented value instead of the original input value. I tried using a temp variable to store the original input but I couldn't get that to work either? Should I try using a separate method to decrement the input?


Increment and decrement are working like they should.

Line 2 day = 5

Line 5 day = 6 because 5 + 1 = 6

Line 7 day = 5 because 6 - 1 = 5
Last edited on
Topic archived. No new replies allowed.