This is the assignment:

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 a lot of code so far, but the only problem that I can't figure out is this: I don't know how to get my main function to display the day of the year that the user inputs in the correct month-day format. Here is my code:

#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>

using namespace std;

// Static member variable type string to translate from integer to month-day format

// Class declaration
class DayOfYear
{
public:
    int day;
    string Month;

    // Constructor function
    DayOfYear(int dayEntered)
    {
        day = dayEntered;
        //cout << "Please enter a number from 1 to 365" << endl;
        //cin >> dayEntered;

    } // end constructor function

    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 print function


}; // end Class DayOfYear



int main()
{
    // How do I get the main function to display the appropriately formatted month day configuration
    // for the day that the user chooses?
    DayOfYear ();

    print();

    cin.ignore();
    cin.get();

    return 0;

}
Member Avatar for iamthwee

You mean...

#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>

using namespace std;

// Static member variable type string to translate from integer to month-day format

// Class declaration
class DayOfYear
{
public:
    int day;




    void print(void)
    {

            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 print function


}; // end Class DayOfYear



int main()
{
    // How do I get the main function to display the appropriately formatted month day configuration
    // for the day thyat the user chooses?
    DayOfYear test;
    test.day = 365;

    test.print();



    cin.ignore();
    cin.get();

    return 0;

}

I already figured out how to display a day that I specify in the code, but I need the program to display a day in that the user enters in that format. Your modified program just displays "December31." I want the user to be able to enter a day of the year and then have the date displayed to them.

Member Avatar for iamthwee

perhaps...

#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>

using namespace std;

// Static member variable type string to translate from integer to month-day format

// Class declaration
class DayOfYear
{
public:
    int day;

    void setDay(int a)
    {
      day = a;
    }


    void print(void)
    {

            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 print function


}; // end Class DayOfYear



int main()
{
    // How do I get the main function to display the appropriately formatted month day configuration
    // for the day thyat the user chooses?

    int day;
    cout << "please enter day:";
    cin>>day;

       DayOfYear test;
       test.setDay(day);

       test.print();
       cout << endl;



    cin.ignore();
    cin.get();

    return 0;

}

my output:

x@x-Calistoga-ICH7M-Chipset:~/Documents/cpp$ g++ -Wall try.cpp
x@x-Calistoga-ICH7M-Chipset:~/Documents/cpp$ ./a.out
please enter day:25
January 25

Thank you! I literally just figured it out, and we both came up with very similar code. Thank you very much!

Member Avatar for iamthwee

As an aside, to obtain top marks you have to make sure you have met these criterion:

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.

Please note my example, was just that, an example. You would NEED to create a constructor to take a parameter as an integer.

You might want to read the following or refer to your notes.

http://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm

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.