April 6, 2006

I have school project that I am working on. I got it to run and it displays the first form. The problem is that I am having trouble figuring out how to get to to display all three forms I need. Here are the 3 forms:

                    12/25/2005
                    December 25, 2005
                    25 December 2005

I got it to display 12/25/2005. I'm trying to figure out how to get it to dislplay the last 2 forms. Below is what I have so far:

//This program demonstrates a simple class.
#include <iostream>
using namespace std;

const int MONTH_SIZE = 2;
const int DAY_SIZE = 2;
const int YEAR_SIZE = 5;

// Date class declaration.
class Date
{
    private:
        double month;
        double day;
        double year;
    public:
        void setMonth(double);
        void setDay(double);
        void setYear(double);
        double getMonth();
        double getDay();
        double getYear();
};

// setMonth assigns its argument to the private member month.

void Date::setMonth(double m)
{
    month = m;
}

// setDay assigns its argument to the private member day.

void Date::setDay(double d)
{
    day = d;
}

// setYear assigns its argument to the private memter year.

void Date::setYear(double y)
{
    year = y;
}

// getMonth returns the value in the private member month.

double Date::getMonth()
{
    return month;
}

// getDay returns the value in the private member  day.

double Date::getDay()
{
    return day;
}

// getYear returns the value in the private member year.

double Date::getYear()
{
    return year;
}

// Function main

int main()
{
    Date box;                  // Define an instance of the class.
    double dateMonth,          // Local variable for month.
           dateDay,            // Local variable for day.
           dateYear;           // Local variable for year.

    // Get the date from the user.
    cout << "Now enter the date. \n";
    cout << "Month (up to 2 digits): ";
    cin >> dateMonth;
    cout << "Day (up to 2 digits): ";
    cin >> dateDay;
    cout << "Year (up to 4 digits): ";
    cin >> dateYear;

    // Store the month, day and year in the box object.
    box.setMonth(dateMonth);
    box.setDay(dateDay);
    box.setYear(dateYear);

    // Display results.
    cout << "Date: ";
    cout << box.getMonth() << "/";
    cout << box.getDay() << "/"; 
    cout << box.getYear() << endl;
    system("pause");
    return 0;
}

I need help! :confused:

Janito2008

Edited 3 Years Ago by mike_2000_17: Fixed formatting

>The problem is that I am having trouble figuring out how to get to to display all three forms I need.

Couldn't you store the months as strings in a vector or map, and use the integer 1-12 to spit out the corresponding month.

Ps, use code tags

why are year, month and day doubles? why not int or long? if you use the functions in <ctime> it would make your class a lot smarter -- for example the formatting you desire can be easily accomplished with strftime(). Once you know year, month and day, fill in a struct tm object, call mktime() to get the size_t time variable then call strftime() to format it however you want. make sure you memset() the struct tm object with all 0s before using it or mktime() may return errors or wrong information.

This article has been dead for over six months. Start a new discussion instead.