I having a problem with my output, it is correct, my problem is a formatting thing.
This is my output
: January 1, 1
d2(12,27,1992): December 27, 1992
d3(0,99,10000): January 1, 1
d1.setDate(2,28,1992): February 28, 1992
d4(2.29,2000): February 29, 2000
d5(2,29,2001): February 1, 2001
and this is the desired output
January 1, 1
d2(12,27,1992): December 27, 1992
d3(0,99,10000): January 1, 1
d1.setDate(2,28,1992): February 28, 1992
d4(2.29,2000): February 29, 2000
d5(2,29,2001): February 1, 2001
notice the colon before January 1, 1 should NOT be there
but the others should
here is my code, maybe someone can see something
below is the main
Code:
int main()
{
// if no args supplied, use default date of January 1, 1
Date d1;
d1.print();
cout << endl;
Date d2(12,27,1992, "d2(12,27,1992)");
d2.print();
cout << endl;
Date d3(0,99,10000, "d3(0,99,10000)");
d3.print();
cout << endl;
d1.setDate(2,28,1992, "d1.setDate(2,28,1992)");
d1.print();
cout << endl;
Date d4(2,29,2000, "d4(2.29,2000)");
d4.print();
cout << endl;
Date d5(2,29, 2001, "d5(2,29,2001)");
d5.print();
cout << endl
return 0;
}
below is the implementation
Code:
int leapYear(int y)
{
if (y % 4 == 0 && y % 100 || y % 400 == 0)
return 1;
else
return 0;
}
const int days [] [13] =
{
{0,31,28,31,30,31,30,31,31,30,31,30,31}, //non-leap year
{0,31,29,31,30,31,30,31,31,30,31,30,31} //leap year
};
const char *months[] =
{"Invalid", "January", "February", "March" ,
"April", "May", "June", "July", "August",
"September", "October", "November", "December"
};
Date::Date(int m, int d, int y, const char *name)
{
setDate(m, d, y, name);
}
Date::~Date()
{
delete [] theName;
}
void Date::setString(const char *str)
{
theName = new char[strlen(str) + 1];
assert(theName != 0);
strcpy(theName, str);
}
void Date::setDate(int m, int d, int y, const char *name)
{
theMonth = (m >= 1 && m <= 12) ? m : MM;
theYear = (y >= 0 && y <= MAX_YY) ? y : YY;
theDay = (d >= 1 && d <= 31) ? d : DD;
if (theDay > days[leapYear(theYear)] [theMonth])
theDay = DD;
setString(name);
}
void Date::print() const
{
cout << theName << ": "<< months[theMonth] << " " << theDay
<< ", " << theYear;
}