Assuming that a year has 365 days, 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. For example,

Day 2 would be January 2
Day 32 would be Febuary 1
Day 365 would be December 31

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 shuld have an integer member variable to represent the day, and shuld have static member variables of type string to assist in the translation from the integer format to the month-day format.

and this is waht i have got to set it up, but cannot think of how to complete it. Please help =(

#include <iostream>
#include <string>

using namespace std;

class DayOfYear
{
public:
	DayOfYear(int d);
	void print();

	static string month[12];

private:
	int day;
	
};

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

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


}

void DayOfYear::print()
{
	cout << day;
}




int main()
{
	DayOfYear test(12);

	test.print();



	cin.ignore();
	cin.get();

	return 0;
}

Recommended Answers

All 8 Replies

Can you do something with an if then ladder that marks the beginning of each month similar to a grade calculation?

/* for grading scale */
char grade;
int score;

if (score > 90)
    grade = 'A';
if (score > 80)
    grade = 'B';
/* ...etc... */

Then just take the difference between the boundary value and the input value.

Or perhaps have an array of month lengths and some sort of accumulator?

I guess it doesnt really say, so I assume yes, but my biggest struggle is how I would convert variable day = 32 into febuary 1, or say variable day = 33 into febuary 2. Thanks for the reply.

For the moment, don't worry about the specific day of the month.

Start simple, take my grading scale example and try to apply it to the months of the year.

Hint:
Which day of the year (numerically) marks the beginning of each month? You've already answered it for Jan. and Feb.

So basically do a few if statments that check a range of variable day, then cout using a math expression for the day and the element of the month array?

Sounds like you have the right idea. If you have an array that holds the count of the days in each month, you could probably do it with some sort of loop as well.

Start month at 1 (January)
If your day > #days-in-January
Subtract #days-in-January from your day, add one to month.
If your day > #days-in-February
Subtract #days-in-February from your day, add one to month.
etc.
The first time day <= #days-in-'month', you have your answer.

Set up a loop and an array to do this.

Start month at 1 (January)
If your day > #days-in-January
Subtract #days-in-January from your day, add one to month.
If your day > #days-in-February
Subtract #days-in-February from your day, add one to month.
etc.
The first time day <= #days-in-'month', you have your answer.

Set up a loop and an array to do this.

Interesting approach, I was actually thinking a decrementing loop starting with December and working backward. Then once you find a month's first day that's less than the current day, you get the month and subtract to get the day.

I figured it out guys, thanks for the replys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.