Program that changes numbers to day of year

I 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. The constructor for the class should take as parameter an integer representing the day of the year, and the class should have a member function print() that prints the day in the month day format. The class should have an integer member variable to represent the day, and should have static member variables holding strings that can be used to assist in the translation from the integer format to the month- day format. I compile my program and I get the following error messages:

In function 'void print()':
error: 'day' was not declared in this scope
error: 'day' was not declared in this scope
In function 'int main()':
error: no matching function for call to 'DayofYear::DayofYear()'
note: candidates are:
note: DayofYear::DayofYear(int)
note: candidate expects 1 argument, 0 provided
note: DayofYear::DayofYear(const DayofYear&)
note: candidate expects 1 argument, 0 provided
error: no match for call to '(DayofYear) (int&)'


Any help would be appreciated, thanks.
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
#include <iostream>
using namespace std;

class DayofYear
{
private:
    int day;
public:
    static const int MonthDays[];
static const string MonthName[];
    DayofYear(int);
    void print();
};

//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"};

DayofYear::DayofYear(int d)
{
    day=d;
}

void 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()
{
DayofYear dYear;
int numberDay;

cout << "Input a number between 1 and 365 and its corresponding";
cout << " day and month of the year will be printed: ";
cin >> numberDay;

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

dYear(numberDay);
dYear.print();
return 0;
}
Line 25: Me thinks you're forgetting a DayofYear:: there.

-Albatross.
ha, thanks that's an obvious one. I am still getting the errors in main though not sure exactly what I did wrong and how to fix it.
Last edited on
Several things.

Line 38: C++ expects you to define a default constructor (one that takes no arguments) if you've declared a non-default constructor and this line is to work.

Also, remember that you can have variable declarations after statements in C++ and it's perfectly valid, meaning you could move the declaration down to line 51 and initialize it there, cutting out the default constructor issue.

Line 48: This check merely spits a warning and keeps going as if nothing happened. The program should either loop back to the entry point or abort at this point.

Did that make sense?

-Albatross
Yes, it did. Problem's solved now, thanks for the help
Topic archived. No new replies allowed.