Question

I need to create a driver and header file in c++ for the following program: Day...

I need to create a driver and header file in c++ for the following program:

Day of the Year

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 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 month–day format.

The program should reject values outside of the range of 1 and 365. DO NOT USE A LOOP, simply display an “Invalid day entered” message.

DO NOT CHANGE THE NAME OR LOCATION OF ANY OF THE FILES PROVIDED. USE ONLY THOSE FILES. ALSO USE THE FUNCTION DEFINITIONS AS WRITTEN

0 0
Add a comment Improve this question Transcribed image text
Answer #1

dayofyear.h

#include <iostream>
class DayOfYear
{
private:
int day; //class variable
public:
DayOfYear(int day) //parametrized constructor
{
this->day=day;
}
void print() //method to print date in month-day format from the given integer
{
std::string months[]={"January","February","March","April","May","June",
"July","August","September","October","November","December"}; //storing name of months
int days[]={31,28,31,30,31,30,31,31,30,31,30,31}; //storing number of days in each month
int month=0,daysBefore=0; //to store current month and cumulative days till last month
while(month<12)
{
if(day<=daysBefore+days[month])
{
std::cout << months[month] << " " << day-daysBefore << "\n"; //output
return; //terminating function
}
daysBefore+=days[month];
month++;
}
std::cout << "Not a valid date\n"; //error message
}
};

dayofyear.cpp

#include <iostream>
#include "dayofyear.h"
using namespace std;

int main()
{
int day;
cout << "Enter the day in integer: ";
cin >> day;
if(day<1 || day>365)
{
cout << "Invalid day entered\n";
return 1;
}
DayOfYear d(day);
d.print();
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
I need to create a driver and header file in c++ for the following program: Day...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT