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

Dani AI

Generated

A compact, robust solution that complements the suggestions from (range checks / month boundaries) and (subtract month lengths) is to store the first day number for each month and use a small search to pick the month. The implementation below keeps the original integer day, uses static string data for month names, validates the input, and avoids common off-by-one mistakes by using a start-of-month array.

#include <iostream>
#include <string>
#include <array>
#include <algorithm>
#include <stdexcept>

class DayOfYear {
public:
    DayOfYear(int d) : dayOfYear(d) {
        if (d < 1 || d > 365) throw std::out_of_range("day must be 1..365");
        auto it = std::upper_bound(startDay.begin(), startDay.end(), d);
        monthIndex = static_cast<int>(it - startDay.begin()) - 1; // 0-based
        dayOfMonth = d - startDay[monthIndex] + 1;
    }

    void print() const {
        std::cout << monthName[monthIndex] << " " << dayOfMonth << '\n';
    }

private:
    int dayOfYear;
    int monthIndex;
    int dayOfMonth;

    static const std::array<std::string,12> monthName;
    static const std::array<int,12> startDay;
};

const std::array<std::string,12> DayOfYear::monthName =
    {"January","February","March","April","May","June","July","August","September","October","November","December"};

const std::array<int,12> DayOfYear::startDay =
    {1,32,60,91,121,152,182,213,244,274,305,335};

int main() {
    try {
        DayOfYear a(2);   a.print();   // January 2
        DayOfYear b(32);  b.print();   // February 1
        DayOfYear c(365); c.print();   // December 31
    } catch (const std::exception& e) {
        std::cerr << e.what() << '\n';
    }
}

Notes: 1) The startDay array lists each month's first day (non-leap year). 2) Off-by-one bugs usually come from using 0-based vs 1-based day counts—this code explicitly accounts for that. 3) If exceptions are not desired, replace the range check with clamping or a validity flag. 4) For leap-year support add a constructor parameter or detect year and adjust startDay/total to 366. This approach is O(1) (small fixed work) and is easier to read and maintain than a long if-then ladder.

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.