Day of the Year class

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 get the following error :\program files (x86)\microsoft visual studio 10.0\vc\bin\test_chapter 14\test_chapter 14\test_chapter 14.cpp(60): error C2660: 'DayOfYear::print' : function does not take 1 arguments
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped Can anyone help me.

// Test_chapter 14.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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); ***this is were the error is*****


system("pause");
return 0;

}



in future use code tags please.

in your function change it to:

void DayOfYear::print(int)

The problem here is that you need an object to print.

You can't just say DayOfYear::print, you have to give it a DayOfYear that you want to print.

Something like this:

1
2
3
4
5
6
DayOfYear obj;

 // .. here you would set obj.day somehow .. probably with a setter function
  //  although you don't have a setter function yet so you might have to write one.

obj.print();  // call it like this 
Last edited on
Zap,

Sorry very new to programing and to this fourm.. Old Mustangs is my forte. I beleive code taging is having your code look like others i've seen here but if you don't mind can you please tell me how to do thaT. Again thank you for the help.
the erro I keep getting is error C2352: 'DayOfYear::print' : illegal call of non-static member function
c:\program files (x86)\microsoft visual studio 10.0\vc\bin\test_chapter 14\test_chapter 14\test_chapter 14.cpp(21) : see declaration of 'DayOfYear::print'
All,

got the code tage thing to work... Here is my latest code but still get error above.


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

#include "stdafx.h"
#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(int day);
};

//**************************************************
// 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()
{
	//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);  //Here is the Issue


	system("pause");
	return 0;

}

Right -- you cannot call member functions like this:

 
DayOfYear::print();


That's wrong.

You need an object from which to call the member function. The way it works is that an object represents 1 instance of the class.


Think of std::string. You know how string has a length function?

1
2
3
string foo = "whatever";

cout << foo.length();  // prints 8, the lenght of 'foo' 


length is a member function. In order for it to work, you need to give it an "object". That object is the string from which it will calculate the length. Here, our object is 'foo'.

What you're trying to do is similar to this:

 
cout << string::length();  // this makes no sense 


The above will give you an error. What string are you getting the length of? It doesn't make any sense.


What you need to do is what I suggested in my previous post. You need to create an object and then call print on that object:

1
2
3
DayOfYear myobj;  // now you have an object

myobj.print();  // then call print like this.  This will print 'myobj' 

Last edited on
ok I get what you are saying but where do I put it in the program. can you past into current program to show me?

thank you for all th help. I admire those that truely understand C++

Steve
Well for starters... change your print function back to how it was. It shouldn't take 'day' as a parameter. It should take no parameters.

Then, in main, you'll need to create a DayOfYear object. You can do it anywhere in main(), as long as it's above the line where you try to print it.

Then, you'll need to set the object's date somehow. For this, you'll need to write a setter function so that you can do something like the below. After that you just print it:

1
2
3
4
5
DayOfYear mydate;  // create the object

mydate.set_day( day );  // set the object's day to the day the user gave us

mydate.print();  // then you can print it 


I'll leave it up to you to write the set_day function.
Topic archived. No new replies allowed.