-
Date Program Help!!
Hi, im trying to make this program to this monday so i need your help rush plz!. First this program has 2 .cpp and 1 .h; the purpose of this program is to enter day,month and year using "Date.h and Date.cpp " from my professor. i have 2 problems first if you enter any month the program always says "11"; in the same way for year always says 2010. maybe is a small error but i can't see it thanks for all!
-
The functions you've made to check the validity of date ranges have many errors in them and are the causes of your two (stated) problems. They also add unnecessary clutter to your class, and seem to be redundant since you perform similar checks (which are similarly broken) again right after you call them.
As a specific example, the logic in the excerpt below is incorrect because it excludes the range of numbers between 1 and 12 as valid values. And you probably wanted to assign the result of checkMonth() to "this->month," not "this->day," though I recommend you remove those checking functions entirely and just do the checking in the setXXX functions.
Code:
void Date::setMonth(int tempMonth)
{
this-> day = checkMonth(tempMonth); //checkmonth ?
if (tempMonth <1 || tempMonth >12)
{
month = tempMonth;
}
else
throw invalidMonth ();
}
Try this instead:
Code:
if(tempMonth >= 1 && tempMonth <=12)
The functions for the other parts of the date are broken in the same way.
Your copy constructor could be deleted, since it isn't doing anything that the default copy constructor wouldn't (the compiler makes one for you if one is not supplied). Your copy assignment operator seems fine, though is probably also unnecessary - the only data members you have in the class are three integers.
I'm guessing that the try-catch blocks and the operator overload functions have been mandated by your professor. You shouldn't use exception handling to do what you can do using local logic, but keep it in there if it's necessary to satisfy him.
The friend operator overload functions seem to be functional (though they don't perform checking on date ranges as you have them):
Code:
Day/Month/Year: 1/21/2011
La fecha es 21/1/2011
La fecha es 21/1/2011
Finally, in Date.h, please change your constructor declaration to something like:
Code:
Date(int m=11,int d=17,int y=2010);
-