Day of Year Program

I am have to create a program for my class. The instructions are to allow a user to input an integer and display the month and day that corresponds to the entered number. For example, user enters 32 and it will display February 1. I must create a DayOfYear class and create a function called print() to display the output message. I am a little lost and need some help. I compile my program and I get the following error message:

dayofyear.cpp(54) : error C2660: 'DayOfYear::print' : function does not take 1 arguments

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
#include <iostream>
#include <string>
using namespace std;


//Day of the year class declaration
class DayOfYear
{
private:
	int day;

public:
	static const int MonthDays[];
	static const string MonthName[];
	void print();
};

//**************************************************
// This function displays the month and day using  *
// the number entered.                             *
//**************************************************

void DayOfYear::print()
{
	int month = 0;

	while (DayOfYear::MonthDays[month] < day)
		month = (month + 1) %12;

		//Display month and day
		cout << DayOfYear::MonthName[month] << " " << day - DayOfYear::MonthDays[month-1];
};


int main()
{
	//Set days of each month into an array
	const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	//Set the name of each month into an array
	const string MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	
	int day;
	//Ask user the total day number
	cout << "\nEnter a number you would like to convert into a month and day";
	cin >> day;

	//Error check for negative numbers and numbers higher than one year
	if(day <= 0 || day > 365)
	{
		cout << "You must enter a valid number (1 thru 365)" << endl;
	}

	//Send entered day number to the print() function
	DayOfYear::print(day);
	return 0;

}
You're very close. You'll need to modify your print function to take a parameter, then remove the class variable day.. This is what you are looking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class DayOfYear
{
private:

public:
	static const int MonthDays[];
	static const string MonthName[];
	void print(int);
};

void DayOfYear::print(int day)
{
	int month = 0;

	while (DayOfYear::MonthDays[month] < day)
		month = (month + 1) %12;

		//Display month and day
		cout << DayOfYear::MonthName[month] << " " << day - DayOfYear::MonthDays[month-1];
};
Thank you for your input. I get this message after making the changes you recommended:

dayofyear.cpp(53) : error C2352: 'DayOfYear::print' : illegal call of non-static member function
dayofyear.cpp(14) : see declaration of 'DayOfYear::print'
Last edited on
Try placing a static modifier in front of void print(int); and void DayOfYear::print(int day)
I get these messages with putting static void print(int);

1>DayOfYear.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const * const DayOfYear::MonthName" (?MonthName@DayOfYear@@2QBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)
1>DayOfYear.obj : error LNK2001: unresolved external symbol "public: static int const * const DayOfYear::MonthDays" (?MonthDays@DayOfYear@@2QBHB)
Now you are running into other compiler errors that don't seem to be closely related to your original question. The wonderful process of debugging begins :]
Can someone please help me?

I used the DayOfYear object and got this error message:

1>DayOfYear.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const * const DayOfYear::MonthName" (?MonthName@DayOfYear@@2QBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)

1>DayOfYear.obj : error LNK2001: unresolved external symbol "public: static int const * const DayOfYear::MonthDays" (?MonthDays@DayOfYear@@2QBHB)

Thanks everyone for all your help!

My current code is:
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
#include <iostream>
#include <string>
using namespace std;


//Day of the year class declaration
class DayOfYear
{
private:

public:
	static const int MonthDays[];
	static const string MonthName[];
	void print(int);
};

//**************************************************
// This function displays the month and day using  *
// the number entered.                             *
//**************************************************

void DayOfYear::print(int day)
{
	int month = 0;

	while (DayOfYear::MonthDays[month] < day)
		month = (month + 1) %12;

		//Display month and day
		cout << DayOfYear::MonthName[month] << " " << day - DayOfYear::MonthDays[month-1];
};


int main()
{
	DayOfYear dYear;

	//Set days of each month into an array
	const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	//Set the name of each month into an array
	const string MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	
	int day;
	//Ask user the total day number
	cout << "\nEnter a number you would like to convert into a month and day";
	cin >> day;

	//Error check for negative numbers and numbers higher than one year
	if(day <= 0 || day > 365)
	{
		cout << "You must enter a valid number (1 thru 365)" << endl;
	}
	
	//Send entered day number to the print() function
	dYear.print(day);
	return 0;

}
You don't ever define these:

1
2
static const int MonthDays[];
static const string MonthName[];
You must reserve space for all static class variables outside of the class.

Move the following from within main() (see below) and prefix DayOfYear:: to them.

1
2
3
4
5
	//Set days of each month into an array
	const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	//Set the name of each month into an array
	const string MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};


to


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

//Day of the year class declaration
class DayOfYear
{
private:

public:
	static const int MonthDays[];
	static const string MonthName[];
	void print(int);
};

// RESERVE space for all static class variables

//Set days of each month into an array
const int DayOfYear::MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//Set the name of each month into an array
const string DayOfYear::MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};


Topic archived. No new replies allowed.