Trouble Overloading Operators: DayOfYear

My program has a constructor that takes number 1-365 and returns month and day of year, and another constructor that takes a month and day as parameters and returns the number 1-365. I also need to overload the postfix/prefix increment/decrement operators.

When the date is December 31, and it is incremented its not incrementing to January 1. Nor will it decrement to Dec. 31 from Jan 1.

Also, in the two arg constructor when I print out the date after the increment/decrement operators are called it is changing the original value instead of the new value.

Can anyone provide me a hint as to what I'm missing or doing wrong?

Any help or advice would be appreciated. Thank you!

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include<iostream>
#include<string>
#include <cstdlib>

using namespace std;

class DayOfYear
{
    private:

        static string month;
        static string months[12];

    public:
        int day;
        DayOfYear();
        DayOfYear(int d);
        DayOfYear(string m, int d);

        void print();

        DayOfYear& operator++();
        DayOfYear operator++(int);
        DayOfYear& operator--();
        DayOfYear operator--(int);
};

string DayOfYear::month;
string DayOfYear::months[12] =
{"January", "February", "March", "April", "May", "June", "July", "August","September", "October","November", "December"};

//Overloaded Operators
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;
}

DayOfYear::DayOfYear()
{
    cout << "Enter the a day of the year (1-365), to print out the month and day\n"
     "or enter a number <=0 to end:" << endl;
    cin >> day;
    print();
}

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

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

    cout << "Day number " << day << " of the year is: ";
    print();

    cout << endl<< "The day after the overloaded prefix increment operator is called is: ";
    ++day;
    print();

    cout << endl << "The day after the overloaded postfix increment operator is called is: ";
    day++;
    print();

    cout << endl << "The day after the overloaded prefix decrement operator is called is: ";
    --day;
    print();

    cout << endl << "The day after the overloaded postfix decrement operator is called is: ";
    day--;
    print();
}




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


    if (m == months[0])
    {
        dayOfYear = d;
    }
     else if (m == months[1])
    {
        dayOfYear = (d + 31);
    }
     else if (m == months[2])
    {
        dayOfYear = (d + 59);
    }
    else if (m == months[3])
    {
        dayOfYear = (d + 90);
    }
    else if (m == months[4])
    {
        dayOfYear = (d + 120);
    }
    else if (m == months[5])
    {
        dayOfYear = (d + 151);
    }
    else if (m == months[6])
    {
        dayOfYear = (d + 181);
    }
    else if (m == months[7])
    {
        dayOfYear = (d + 212);
    }
    else if (m == months[8])
    {
        dayOfYear = (d + 243);
    }
    else if (m == months[9])
    {
        dayOfYear = (d + 273);
    }
    else if (m == months[10])
    {
        dayOfYear = (d + 304);
    }
    else if (m == months[11])
    {
        dayOfYear = (d + 334);
    }

       if (m == months[0] && d > 31){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
            }
        else if (m == months[1] && d > 28){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
            }
        else if (m == months[2] && d > 31){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if (m == months[3] && d > 30){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if (m == months[4] && d > 31){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if (m == months[5] && d > 30){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if(m == months[6] && d > 31){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if (m == months[7] && d > 31){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if(m == months[8] && d > 30){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if(m == months[9] && d > 31){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if(m == months[10] && d > 30){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if(m == months[11] && d > 31){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }
        else if (d <=0){
            cout << "Invalid input, terminating program." << endl;
                exit(0);
        }




    cout << month << " " << day << " is day number " << dayOfYear << " of the year." <<endl;



    cout << endl<< "The day of the year after the overloaded prefix increment operator is called is: " << ++dayOfYear;

    cout << endl << "The day of year after the overloaded postfix increment operator is called is: " << dayOfYear++;

    cout << endl << "The day of year after the overloaded prefix decrement operator is called is: " << --dayOfYear;

    cout << endl << "The day of year after the overloaded postfix decrement operator is called is: " << dayOfYear--;

}



void DayOfYear::print() {
    if( day > 366){
        cout << "Invalid input, terminating program. Day is greater than 365." << endl;
        exit(0);
    }
    else if( day < 0){
        cout << "Invalid input, terminating program. Day is less than 1." << endl;
        exit(0);
    }
    else if (day > 0 && day < 32)
    {
        month = months[0];
        cout << month << " " << 31 + day - 31 << endl;
    }
    else if (day > 31 && day < 60)
    {
        month = months[1];
        cout << month << " " << day - 31  << endl;
    }
    else if (day > 59 && day < 91)
    {
        month = months[2];
        cout << month << " " << day - 59 << endl;
    }
    else if (day > 90 && day < 121)
    {
        month = months[3];
        cout << month << " " << day - 90 << endl;
    }
    else if (day > 120 && day < 152)
    {
        month = months[4];
        cout << month << " " << day - 120 << endl;
    }
    else if (day > 151 && day < 182)
    {
        month = months[5];
        cout << month << " " << day - 151 << endl;
    }
    else if (day > 181 && day < 213)
    {
        month = months[6];
        cout << month << " " << day - 181 << endl;
    }
    else if (day > 212 && day < 244)
    {
        month = months[7];
        cout << month << " " << day - 212 << endl;
    }
    else if (day > 243 && day < 274)
    {
        month = months[8];
        cout << month << " " << day - 243 << endl;
    }
    else if (day > 273 && day < 305)
    {
        month = months[9];
        cout << month << " " << day - 273 << endl;
    }
    else if (day > 304 && day < 335)
    {
        month = months[10];
        cout << month << " " << day - 304  << endl;
    }
    else if (day > 334 && day < 366)
    {
        month = months[11];
        cout << month << " " << day - 334 << endl;
    }
}

int main()
{

    int input;
    string month;
    int day;


    cout << "Enter a number 1-365 to see the corresponding day of the year.";
    cin >> input;

    DayOfYear * dayOfYear1 = new DayOfYear(input);

/*
    cout << endl << endl;
    cout << "Now enter a month and day to see the corresponding day number." << endl;
    cout << "First enter the month:";
    cin >> month;
    cout << endl << "Now enter the day: ";
    cin >> day;

    DayOfYear * dayOfYear2 = new DayOfYear(month, day);

*/

    system("PAUSE");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.