Thread: Help with Date Class

  1. #1
    Registered User
    Join Date
    Feb 2009

    Help with Date Class

    I'm trying to create a Date Class to run with some predetermined input and I am having a tought time with a few areas.

    I haven't coded in C or C++ for awhile so I have a feeling this code is going to look really ugly to some people, so any other suggestions to clean it up are more than welcome.

    Basically I'm not sure how I should get the getMonthName() function to work to print out the name of the month. I believe the way I declared it in my class header and member-function is incorrect.

    All help is appreciated.

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    class Date 
    	int month;
    	int day;
    	int year;
    	Date( int mn, int dy, int yr );
    	int getDay();
    	int getMonth();
    	int getYear();
    	void getMonthName();
    	void print() const;
    	void printLong() const;
    Date::Date( int mn, int dy, int yr )
    	if ( mn > 0 && mn <= 12 )
    		month = mn;
    		month = 1;
    	if ( yr >= 1900 )
    		year = yr;
    		year = 1900;
    	static int length[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    	if ( dy == length[month] )
    		day = dy;
    		day = 1;
    int Date::getDay()
    	return day;
    int Date::getMonth()
    	return month;
    int Date::getYear()
    	return year;
    void Date::getMonthName()
    	static char *name[] = { "nothing", "January", "February", "March", "April",
                "May", "June", "July", "August", "September", "October",
                "November", "December" };
    	return name[month];
    void Date::print() const
    	cout << month << day << year;
    void Date::printLong() const
    	cout << day << month << year;
    int main()
        Date d1;             // default ctor
        Date d2(7, 4, 1976); // July 4'th 1976
        Date d3(0, 15, 1880);// Adusted by ctor to January 15'th 1900
        d1.print();         // prints 01/01/2000
        d1.printLong();     // prints 1 January 2000
        cout << endl;
        d2.print();         // prints 07/04/1976
        d2.printLong();     // prints 4 July 1976
        cout << endl;
        d3.print();         // prints 01/15/1900
        d3.printLong();     // prints 15 January 1900
        cout << endl;
        cout << "object d2's day is " << d2.getDay() << endl;
        cout << "object d2's month is "  << d2.getMonth() << " which is " << d2.getMonthName() << endl;
        cout << "object d2's year is " << d2.getYear() << endl;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    If you want to have a "return" statement in your function, it sure as heck can't be void. Presumably you meant to have an array of strings and return something of string type.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    I hate to say it but that date class just isn't useful. It is nothing more than a container for some values, and a formatter for formating them specifically to cout. Add to that the fact that its broken for leap years, (assuming you fix the compile errors), and it has very little real world value.

    To simply "clean it up" really wont cut it. Study something like COleDateTime for an idea of some of the stuff that such a class needs to do to be useful.

    I will show some ways of improving this function though:
    std::string Date::getMonthName() const
        static const char * const name[] = { "January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October", "November", "December", };
        return name[month-1];
    Even this function isn't such a good idea though, as it is obviously tied to just English. One can ask the operating system to format the date for you instead, and then it'll work for many languages.

    Take the time to learn about const-correctness when you can.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Feb 2009
    this is the first program for an intro to c++ class im taking. what i have coded is what the professor has asked for so im only following by the rules i was given.

    anyways, thank you for the help. i missed a couple other things too, but the month name is working correctly now. ill heed your advice in my future programming however.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM