Struggling with Day of the Year Program

My assignment is to "write a class named DayOfYear that takes an integer representing a day of the year and translates it to a string consisting of the month followed by day of the month."

Program should have one constructor that takes the day of the year as a parameter and another one that takes the month and day as two parameters. We are supposed to overload the prefix and postfix decrement and increment operators as well.

I have spent a lot of time working with the code so far, but I still can't get anything but the default constructor to run. I know I am supposed to call the two constructors in the main method, but everything I try results in compiler errors.

Can anyone offer some advice that could point me in the right direction? Any help would be appreciated. Thank you!

Here's what I have so far:

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
  #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++();
        DayOfYear operator++(int);
        DayOfYear& operator--();
        DayOfYear operator--(int);
};

string DayOfYear::month;

//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(string m, int d) {
    month = m;
    day = d;

    if (m == "January" && d > 31) {
        cout << "Invalid input, terminating program." << endl;
        exit(0);
    } 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 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;
        cin >> d;
        day = d;
    }
    print();
}


void DayOfYear::print() {
    if( day > 365){
        cout << "Invalid input, terminating program. Day is greater than 365." << endl;
        exit(0);
    }
    else if( day < 1){
        cout << "Invalid input, terminating program. Day is less than 1." << endl;
        exit(0);
    }
    else 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;
    }
}

int main()
{

    DayOfYear day;

    //DayOfYear *dayOfYear = new DayOfYear(day);
    //DayOfYear *dayOfYear = new DayOfYear(month, day);
    //DayOfYear::DayOfYear(string m, int d)

    system("PAUSE");
    return 0;
}

but everything I try results in compiler errors.

What are the errors?

What does the following display?
1
2
DayOfYear * dayOfYear001 = new DayOfYear(1,1);
dayOfYear001->print();
Invalid conversion from int to const char*
What compiler errors?
What does it say?
Invalid conversions
Which line number is listed in the error? Which filename is listed in the error?

This is why it is recommended that you cut and paste the entire error message when seeking help from others.
What does the following display?
1
2
DayOfYear * dayOfYear001 = new DayOfYear(1,1);
dayOfYear001->print();


I don't believe there is a valid constructor for that. It would have to be

new DayofYear("march", 1);//Something like this
mobotus: Thanks for the correction. You are correct.

:-) I guess I want to add a new constructor DayOfYear::DayOfYear(int month, int day); :-)
Last edited on
I got the program to compile! Thanks again to all.

Mobotus this is the second time you've helped me on this same program, much appreciated!


My plan is to nest more if statements in this constructor so it will ultimately convert the month and day passed into the parameter into a int day of the year. (ie (January, 1) will return 1)


Now that I've got the constructor running, when I call

DayOfYear * dayOfYear001 = new DayOfYear("July", 4);
dayOfYear001->print();

It prints July 4 to the console then January 4. Any clue as to what's happening here?
expand it ...
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
#include <iostream>
#include <string>
using namespace std;

class dmy {
	private:
		int total;
		int month;
		int day;		
		string s;
		
	public:
		dmy() {	}
		dmy(int n);
		//dmy(int n1, int n2) { //...	}

		void show () 
		{
			cout << s;
		}
};

dmy::dmy(int n) {
	total =n;

	while (true)
	{
		if (total <=31){
			s= "January " + to_string(total);  break;
		}
		
		else if (total <=59){  //31+28 = 59
			s= "February " + to_string(total - 31);  break;
		}
		
		else if (total <=90){  //59+31 = 90
			s= "March " + to_string(total - 59);  break;
		}

		else if (total <=120){  //90+30 =120
			s= "April " + to_string(total - 90);  break;
		}

		//... other months
		
	}

}

int main() {

	dmy ob1(115); // april 25

	//dmy ob2(2,28); // february 28
	
	ob1.show();
	cout << "\n\n";	

	return 0;
}
Thanks anup! My first try was not quite as organized but it does work:

I added this to the bottom of my two arg constructor

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
    if (m == "January")
    {
        dayOfYear = d;
    }
     else if (m == "February")
    {
        dayOfYear = (d + 31);
    }
     else if (m == "March")
    {
        dayOfYear = (d + 31+28);
    }
    else if (m == "April")
    {
        dayOfYear = (d + 31+28+31);
    }
    else if (m == "May")
    {
        dayOfYear = (d + 31+28+31+30);
    }
    else if (m == "June")
    {
        dayOfYear = (d + 31+28+31+30+31);
    }
    else if (m == "July")
    {
        dayOfYear = (d + 31+28+31+30+31+30);
    }
    else if (m == "August")
    {
        dayOfYear = (d + 31+28+31+30+31+30+31);
    }
    else if (m == "September")
    {
        dayOfYear = (d + 31+28+31+30+31+30+31+31);
    }
    else if (m == "October")
    {
        dayOfYear = (d + 31+28+31+30+31+30+31+31+30);
    }
    else if (m == "November")
    {
        dayOfYear = (d + 31+28+31+30+31+30+31+31+30+31);
    }
    else if (m == "December")
    {
        dayOfYear = (d + 31+28+31+30+31+30+31+31+30+31+30);
    }

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


It works, but it is still printing January "X" to output after the result. I know the source is the print function but still trying to figure it out..
Last edited on
It prints July 4 to the console then January 4. Any clue as to what's happening here?


Look at your print method.

DayOfYear * dayOfYear001 = new DayOfYear("July", 4);
cout is used in the constructor and month and day are printed
day is assigned 4

dayOfYear001->print();
Print is called on the obj. and we figure out what to print based on the day var. which was assigned 4 in the previous statement
Last edited on
You da man mobotus!

Thanks to all for the help, couldn't have done it without your advice. It's finally starting to come together nicely.
Registered users can post here. Sign in or register to post.