Assuming that a year has 365 days, write a class named DayOfYear that takes an i
ID: 3575268 • Letter: A
Question
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 February 1
Day 365 would be December 31.
Here is what I have done so far but I don't know why am i getting compiler errors and also I dont know if wrote the static variables right
// Chapter 14, Programming Challenge 2: Day of the Year
// Insert include statements
#include <iostream>
#include <string>
#include "DayOfYear.h"
using namespace std;
int main()
{
int day;
// Display the purpose of the program.
cout << "This program converts a number "
<< "into a string representing the "
<< "month and day. ";
// Get the day as input from the user.
cout << " Enter a whole number between 1 and 365: ";
cin >> day;
// Create an instance of the DayOfYear class / Set the day.
DayOfYear date;
// Display the object.
date.print(day);
//cout << "The result is " << dayMonth << " and " << restDays << endl;
return 0;
}
#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H
#include <iostream>
#include <string>
using namespace std;
class DayOfYear
{
private:
// day
int day;
// static variables
static const string monthName[];
static const int dayNumber[];
public:
// constructor
DayOfYear();
// set (optional)
void print(int);
};
const string DayOfYear::monthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int DayOfYear::dayNumber[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
#endif
Explanation / Answer
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>
using namespace std;
class DayOfYear
{
public:
int day;
String Month;
DayOfYear(int dayEntered)
{
day = dayEntered;
}
void print()
{
if(day >= 1 && day <= 31)
{
cout <<" Day "<<day<<" would be " <<"January " << day << endl;
}
if(day >= 32 && day <= 59)
{
cout <<" Day "<<day<<" would be " <<"February "<< day-31 << endl;
}
if(day >= 60 && day <= 90)
{
cout <<" Day "<<day<<" would be " <<"March"<< day-59 << endl;
}
if(day >= 91 && day <= 120)
{
cout <<" Day "<<day<<" would be " <<"April"<< day-90 << endl;
}
if(day >= 121 && day <= 151)
{
cout <<" Day "<<day<<" would be " <<"May"<< day-120 << endl;
}
if(day >= 152 && day <= 181)
{
cout <<" Day "<<day<<" would be " <<"June" << day -151<< endl;
}
if(day >= 182 && day <= 212)
{
cout <<" Day "<<day<<" would be " <<"July" << day-181 << endl;
}
if(day >= 213 && day <= 243)
{
cout <<" Day "<<day<<" would be " <<"August" << day-212 << endl;
}
if(day >= 244 && day <= 273)
{
cout <<" Day "<<day<<" would be " <<"September" << day -243<< endl;
}
if(day >= 274 && day <= 304)
{
cout <<" Day "<<day<<" would be " <<"October"<< day-273 << endl;
}
if(day >= 305 && day <= 334)
{
cout <<" Day "<<day<<" would be " <<"November"<< day-304 << endl;
}
if(day >= 335 && day <= 365)
{
cout <<" Day "<<day<<" would be " << "December" << day -334<< endl;
}
}
};
int main()
{
int dayEntered;
cout << "Please enter a number from 1 to 365" << endl;
cin >> dayEntered;
DayOfYear instance = DayOfYear (dayEntered);
instance.print();
cin.ignore();
cin.get();
return 0;
}