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