I'm having a problem with a program I'm doing for class. The first part I have finished, it's a program that allows the user to enter a number from 1 to 365, and it outputs what Month and day of the month that number is associated with. My problem arises in the second part of the program in which I need to modify the program so that it allows the user to enter a month and day of the month, and outputs the associated day of the year. Essentially the finished program should allow the user to enter whichever they wish, and receive the opposite as output. I think I need to use an overloaded constructor that accepts a string, and an integer, but I'm not sure where to go from there.

Please show us the code that you've come up with so far and we can help guide you.

Here is the header file

// dayofdyear.h
#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H
#include <iostream>
#include <string>
using namespace std;

class DayOfYear
{
private:
   int day; 

public:
   static const int daysAtEndOfMonth[ ];
   static const string monthName[ ];
   void print();
   void setDay(int d)
   {day = d;} 
	
};


//***************************************
//          DayOfYear::print.           *
// Convert and print day of year        * 
//***************************************                                 
void DayOfYear::print()
{
    int month = 0;
    
	// Calculate the month.   
	while (daysAtEndOfMonth[month] < day)
         month = (month + 1) %12;
	
	// Output the month and the day.
	if (month == 0) 
      cout << "January " << day;
    else
      {
           cout << DayOfYear::monthName[month] <<  " " 
                << day - DayOfYear::daysAtEndOfMonth[month-1];
      }    
};
#endif

Here is the main .cpp file

// pg740_2.cpp
// This program takes an integer representing a day of the 
// Year and translates it to an description of the form
// Month - day of month.
// Assumes all years have 365 days

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


// Note that daysAtEndOfMonth and monthName can be assigned
// values before any objects have been declared because they
// have been declared as static.
const int DayOfYear::daysAtEndOfMonth[ ] = {31, 59, 90, 120, 
                                       151, 181, 212, 243, 273,
                                       304, 334, 365};
const string DayOfYear::monthName[ ]= {"January", "February", "March",
                 "April", "May", "June", "July",
                 "August", "September", "October",
                 "November", "December"};




int main()
{
    DayOfYear dy;
	// Tell user what program does
    cout << "This program converts a day given by a number 1 through 365" << 
            "\ninto a month and a day.";
    // Get user input			
    cout << "\nEnter a number: ";
    int day;
    cin >> day;
    if (day <= 0 || day > 365)
    {
        cout << "Invalid range for a day." << endl;
        exit(1);
    }
    // Do computation and print result
    dy.setDay(day);
    dy.print();
	cout << endl;
    return 0;

This was the first program that we were assigned, the part i'm stuck on is modifying this program to basically work in reverse. So far my only starting point would be to add in an overloaded constructor for setDay, but i'm confused as to where to go from there.

Well take the month as input from the user, and then the day of the month,
Find the corresponding [ month-1] and add the days of the month value to it, to get the required result,

As far as the constructor, you will now need an extra member to save the extra Month variable. preferably an integer.

Though You will now need an additional print function , to perform the reverse operation and give out the output in the form of "No of days".

Yeah, I basically thought that, I suppose most of my confusion has derived from the way the instructions are worded in the instructions are worded. They say this: "...add a constructor that takes two parameters: a string representing a month and an integer in the range of 0 to 31 representing the day of the month. The constructor should then initialize the integer member of the class to represent the day specified by the month and day of month parameters. The constructor should terminate the program with an appropriate error message if the number entered for a day is outside the range of days for the month given." And that's all it says, it didn't mention another print function, or anything like that. It's just pretty vague and confusing.

Yeah, I basically thought that, I suppose most of my confusion has derived from the way the instructions are worded in the instructions are worded. They say this: "...add a constructor that takes two parameters: a string representing a month and an integer in the range of 0 to 31 representing the day of the month. The constructor should then initialize the integer member of the class to represent the day specified by the month and day of month parameters. The constructor should terminate the program with an appropriate error message if the number entered for a day is outside the range of days for the month given." And that's all it says, it didn't mention another print function, or anything like that. It's just pretty vague and confusing.

If classes are new to you and the spec is confusing to you, I'd suggest doing it just in main. Ask the user to enter a month and a day and convert it to a number from 1 to 365. Display the results to check the accuracy. Once you get that working, try integrating it into your class.

That's a good suggestion. I'll try that, sometimes all the extra code gets distracting from what I'm actually needing to do. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.