I have having a bit of trouble with this assignment I have to do. The assignment is:
"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 string consisting of the month followed by day of the month. For example,

Day 2 would be January 2
Day 32 would be February 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 should have an integer member variable to represent the day, and should have static member variables of type string to assist in the translation from the integer format to the month-day format."

I'll post what I have now, even though I am doing it the wrong way. I do not know how to do the translation the way they want it done. I haven't used the string in my program, and I am not familiar with parameters for the constructor or how to use them. Any help would be greatly appreciated.

Challenge 3 Header.h

#include <iostream>
#include <string>
using namespace std;

static string date;
class DayOfYear
{
public:
DayOfYear()
{}
int y;
static string date;
void print();
};

Challenge - 3.cpp

#include "Challenge 3 Header.h"

void DayOfYear::print()
{
		if (y>0 && y<=31)
		{
			cout << "January " << y << endl;
		}
		if (y>31 && y<=59)
		{
			cout << "February " << (y-31) <<endl;
		}
		if (y>59 && y<=90)
		{
			cout << "March " << (y-59) <<endl;
		}
		if (y>90 && y<=120)
		{
			cout << "April " << (y-90) <<endl;
		}
		if (y>120 && y<=151)
		{
			cout << "May " << (y-120) <<endl;
		}
		if (y>151 && y<=181)
		{
			cout << "June " << (y-151) <<endl;
		}
		if (y>181 && y<=212)
		{
			cout << "July " << (y-181) <<endl;
		}
		if (y>212 && y<=243)
		{
			cout << "August " << (y-212) <<endl;
		}
		if (y>243 && y<=273)
		{
			cout << "September " << (y-243) <<endl;
		}
		if (y>273 && y<=304)
		{
			cout << "October " << (y-273) <<endl;
		}
		if (y>304 && y<=334)
		{
			cout << "November " << (y-304) <<endl;
		}
		if (y>334 && y<=365)
		{
			cout << "December " << (y-334) <<endl;
		}
}

Run Challenge 3.cpp

#include "Challenge - 3.cpp"
int main()
{
	DayOfYear DOY;
	cout << "Enter a Day of the Year and then Press Enter" << endl;
	cin >> DOY.y;
	DOY.print();
	system("pause");
	return 0;
}

well you being asked to provide the information in the constructor so you should have something like

class DayOfYear
{
public:
       DayOfYear(int day) { day = y; }
       //...
};

the you initilize the class like

int main()
{
       int day;
       cout << "enter the day: ";
       cin >> day
       DayOfYear(day);
       DayOfYear.print();
       return 0;
}

hope I'm explaining this okay for you.

I am having a hard time understanding. I keep getting errors around these lines of code:

#include "Challenge - 3.cpp"

int main()
{
int day;
cout << "Enter the Day of the Year: ";
cin >> day;
DayOfYear(day);
DayOfYear.print();
system("pause");
return 0;
}

Here are the errors:

run challenge 3.cpp(8) : error C2371: 'day' : redefinition; different basic types

run challenge 3.cpp(5) : see declaration of 'day'

challenge - 3\run challenge 3.cpp(8) : error C2512: 'DayOfYear' : no appropriate default constructor available

challenge - 3\run challenge 3.cpp(9) : error C2143: syntax error : missing ';' before '.'

challenge - 3\run challenge 3.cpp(9) : error C2143: syntax error : missing ';' before '.'


well you being asked to provide the information in the constructor so you should have something like

class DayOfYear
{
public:
       DayOfYear(int day) { day = y; }
       //...
};

the you initilize the class like

int main()
{
       int day;
       cout << "enter the day: ";
       cin >> day
       DayOfYear(day);
       DayOfYear.print();
       return 0;
}

hope I'm explaining this okay for you.

Well, it should be y = day; but I've been guilty of that typo more than once.

I would:

//declare these variables within the class
static const int daysInMonth[12] = {31, 28, 31, 30, //etc
static const string monthNames[12] = {"January", "February", //etc
int y;
string dayString;

//within the constructor declare these variables
int dayOfMonth= y;
int monthIndex = 0;
string dayOfMonthString;

//within the constructor:
//1) use a loop to obtain the monthIndex and the final value of dayOfMonth
while(dayOfMonth > daysInMonth[monthIndex]) 
{
   //calculations required in here
}
//2) convert dayOfMonth into dayOfMonthString using a stringstream
//3) initialize dayString by using monthIndex to access the month name and concatenating the variiable dayOfMonthString onto the month name

sorry in main it should be

//...
DayOfYear DOY(day);
DOY.print();
//...

I am still lost, we haven't discussed the use of String Streams. I also don't understand the while loop and what is to be entered.

Header File:

#include <iostream>
#include <string>
using namespace std;

string dayString;
class DayOfYear
{
public:
static const int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const string monthNames[12] = {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
int day;
string dayString;

DayOfYear()
{
	int dayOfMonth = y;
	int monthIndex = 0;
	string dayOfMonthString;
	//use a loop to obtain the monthIndex and the final value of dayOfMonth
	while(dayOfMonth > daysInMonth[monthIndex])
	{
		//calculations required in here
	}
	void print()
	{
		cout << "Month: " << monthIndex << "Day: " << y << endl;
	}
}
};

CPP

#include "Challenge 3 Header.h"

int main()
{
cout << "Enter the Day of the Year: ";
cin >> day;
DayOfYear DOY(day);
DOY.print();;
system("pause");
return 0;
}

Post #6:
line 12 should be int y;
line 15 should be DayOfYear(int day)
line 17 should be int dayOfMonth = y = day;

Line 23 should be expanded to contain 2 sections.
Section 1:
substract number of days in current month from dayOfMonth and store difference back in dayOfMonth
Section 2:
increment monthIndex

Since you have declared two parallel arrays, daysInMonth and monthNames, then you can use monthIndex to access both the number of days in the month and the name of the month. For example: if monthIndex is 11 that means the month's name is December and the number of days in the month is 31 because those are the values of the elements in the arrays that have index of 11.

If you haven't studied how to convert numerical data into strings then the instructions aren't very good as to me they indicate that you are supposed to output the information as a string, not as a string and an int. If you want/need to output a string and an int then you can display the dayOfMonth, not y. The routine way to convert numerical data into C style strings is with sprintf(). stringstreams will do the same for STL string objects.

Sorry about the late edit

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.