Question: C++ Programming Not sure if I did this correctly, any help would be great! Here is the question: Assuming that a year has 365 days,
C++ Programming
Not sure if I did this correctly, any help would be great!
Here is the 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.
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 monthday format. The class should have an integer member variable to repre-
sent the day and should have static member variables holding string objects that can be used to assist in the translation from the integer format to the month-day format.
Test your class by inputting various integers representing days and printing out their representation in the monthday format.
Use consts where appropriate.
Make sure you are creating objects for a Janurary date, a December date and one of your choice from another month and printing them using the print method.
my code:
// DayOfYear.cpp
#include
string DayOfYear::dayMonth = ""; int DayOfYear::restDays = 0;
DayOfYear::DayOfYear(int d) { day = d; }
void DayOfYear::setEndOfMonth() { EndOfMonth[0] = 0; EndOfMonth[1] = 31; EndOfMonth[2] = 59; EndOfMonth[3] = 90; EndOfMonth[4] = 120; EndOfMonth[5] = 151; EndOfMonth[6] = 181; EndOfMonth[7] = 212; EndOfMonth[8] = 243; EndOfMonth[9] = 273; EndOfMonth[10] = 304; EndOfMonth[11] = 334; EndOfMonth[12] = 365; }
void DayOfYear::setMonthName() { months[0] ="January"; months[1] ="February"; months[2] ="March"; months[3] ="April"; months[4] ="May"; months[5] ="June"; months[6] ="July"; months[7] ="August"; months[8] ="September"; months[9] ="October"; months[10] ="Novemeber"; months[11] ="December"; }
void DayOfYear::print() { int month = 0;
while(EndOfMonth[month] < day) month++; dayMonth += months[month - 1]; restDays += day - EndOfMonth[month - 1];
cout << " The day is: " << dayMonth << " " << restDays << endl; }
//DayOfYear.h
#ifndef DAYOFYEAR_H #define DAYOFYEAR_H
#include
class DayOfYear { private:
int day; string months[12]; int EndOfMonth[13]; static string dayMonth; static int restDays;
public: DayOfYear(int d); void print(); void setEndOfMonth(); void setMonthName(); }; #endif
//Main.cpp
#include
int main() { int dayNumber;
cout << "Please enter a number between 1 and 365: "; cin >> dayNumber;
while (dayNumber < 1 ||dayNumber > 365) { cout << "Invalid entry, Please reenter a number between 1 and 365: "; cin >> dayNumber; }
DayOfYear d(dayNumber);
d.setEndOfMonth();
d.setMonthName();
d.print();
system ("pause"); return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
