in c++ Assuming that a year has 365 days, write a class named DayOfYear that tak
ID: 3581301 • Letter: I
Question
in c++
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 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. Test your class by inputting various integers representing days and printing out their representation in the month-day format. I have everything- I just need to figure out how to get my main function to dispaly the day THAT THE USER INPUTS in the correct month-day format.
Explanation / Answer
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>
#include<conio.h>
using namespace std;
// Class DayOfYear declaration
class DayOfYear
{
public:
int day;
string Month;
// Constructor
DayOfYear(int dayEntered)
{
day = dayEntered;
}
void print()
{
if(day >= 1 && day <= 31)
{
cout << "January" << day << endl;
}
if(day >= 32 && day <= 59)
{
cout << "February" << (day - 31) << endl;
}
if(day >= 60 && day <= 90)
{
cout << "March" << (day - 59) << endl;
}
if(day >= 91 && day <= 120)
{
cout << "April" << (day - 90) << endl;
}
if(day >= 121 && day <= 151)
{
cout << "May" << (day - 120) << endl;
}
if(day >= 152 && day <= 181)
{
cout << "June" << (day - 151) << endl;
}
if(day >= 182 && day <= 212)
{
cout << "July" << (day - 181) << endl;
}
if(day >= 213 && day <= 243)
{
cout << "August" << (day - 212) << endl;
}
if(day >= 244 && day <= 273)
{
cout << "September" << (day - 243) << endl;
}
if(day >= 274 && day <= 304)
{
cout << "October" << (day - 273) << endl;
}
if(day >= 305 && day <= 334)
{
cout << "November" << (day - 304) << endl;
}
if(day >= 335 && day <= 365)
{
cout << "December" << (day - 334) << endl;
}
}
}; // end Class DayOfYear
int main()
{
// choose a day to print
int num;
cout<<"Enter num:";
cin>>num;
DayOfYear day(num); // creating object
day.print();// calling print method from DayofYear class
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.