Good afternoon, i was wondering if anyone can assist me with this problem i am having. I am required to create a member function called void Date::Print() that prints 3 different formats of dates: 5/25/1995 , 05/25/1995, and May 25, 1995.
I need to set these as strings or any type that will allow me to store the data and use it for later because ill have to make a bool Date::SetFormat function as well to change the format. If there is anyone with any ideas, anything can help.
A format arrangement operates on the basic data (properties).
So what this means is you first have to decide what constitutes a date. A date can be a set of 3 numbers, a day number, a month number and a year number.
Also, once you know the month number you can convert the number into a month name eg month[5] = "May".
Put all that together and you can produce the output you require.
if this is for a homework exercise that requires you to create your own date class from scratch then you should follow kermort's advice of storing the date as 3 numbers to represent the year, month and day and create some parser for the format to be applied or alternatively have fixed format styles that can be applied without the need for parsing.
If this is a part required for a bigger project or work related then you can try the following by leveraging the functionality of an already existing date/time class that has a formatter:
MyDate.h
1 2 3 4 5 6 7 8 9 10 11
#include "KDate.h"
class CMyDate : public KLib::KDate
{
public:
KLib::KStr Format(constchar * szFmt) const;
private:
staticconstchar * MonthNames[12];
};
A small point, make month[0] = "ZERO" or something and do away with subtracting 1 to get the appropriate index, purely for convenience, clarity and simplicity.
A small point, make month[0] = "ZERO" or something and do away with subtracting 1 to get the appropriate index, purely for convenience, clarity and simplicity.
Good point, and will also allow the exposing of the MonthNames array as public so as to be used from outside the class in a natural way without having to subtract 1 from the month first. Just ensure that month value passed as index to such an array falls within range ...