Wednesday, December 6, 2017

Programming Challenge 14.2 - Day of the Year

Example Files: DayOfTheYear.7z

DayOfTheYear.h


#ifndef DAY_OF_THE_YEAR_H_
#define DAY_OF_THE_YEAR_H_

#include <string>
using std::string;

const int NUM_MONTHS = 12;

class DayOfYear
{
    private:
        const static string monthOfYear[NUM_MONTHS];
        const static int daysInYear[NUM_MONTHS];
       
        int day;                // To hold a day

    public:
        DayOfYear(int d)
        {
            day = d;
        }

        void print();
};

#endif

DayOfTheYear.cpp


#include "DayOfTheYear.h"

#include <iostream>
using std::cin;
using std::cout;

// Definition of const static array member holding month names
const string DayOfYear::monthOfYear[] = { "January", "February", "March", "April", "May", "June", "July",
                                                      "August", "September", "October", "November", "December" };

// Definition of const static array member holding a range of days
const int DayOfYear::daysInYear[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

/* **********************************************************
            DayOfYear::print()
    This function translates a day to month day format and
    outputs it to screen.
   ********************************************************** */

void DayOfYear::print()
{
    bool isFound = false;
   
    for (int count = 0; count < NUM_MONTHS && isFound != true; count++)
    {
        if (day <= daysInYear[count])
        {
            cout << monthOfYear[count] << " " << (day %= daysInYear[count - 1]) << ".\n";
            isFound = true;
        }
    }
}

DayOfTheYearDm.cpp


#include "DayOfTheYear.h"

#include <iostream>
using std::cin;
using std::cout;

int main()
{
    int number = 0;
    char again = ' ';

    cout << "\t\tDAY OF THE YEAR TO MONTH AND DAY TRANSLATOR\n\n";
    cout << "Please enter a day in the range of 1 through 365, and I will\n"
          << "translate it for you into a month and day format. For instance,\n"
          << "32 becomes February 1., and 230 translates to August 18.\n\n";

    do
    {
        cout << "Please enter a day (1 through 365): ";
        cin >> number;

        while (number <= 0 || number > 365)
        {
            cout << "Please enter a day (1 through 365): ";
            cin >> number;
        }

        // Create a DayOfYear class object
        DayOfYear day(number);
        day.print();

        cout << "\nWould you like me to translate another day (y/N)? ";
        cin >> again;

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            cout << "\nWould you like me to translate another day (y/N)? ";
            cin >> again;
        }
        cout << "\n";

        if (toupper(again) == 'N')
        {
            cout << "\nThank you for trying this demo program, have a nice day!";
        }
    } while (toupper(again) != 'N');

    cin.get();
    cin.ignore();
    return 0;
}

Example Output:




No comments:

Post a Comment