I'm still working on this date class, and I'm up to the point where all my overloaded operators are set up correctly, but I'm having a problem with my dates validating.
Date Date1; //Define an instance for Date
Date Date2; //Another instance of a date
Date Date3; //Holds the difference between
int year; //local variable for year
int month; //local variable for month
int day; //local variable for day
cin >> Date1;
if (Date1.checkDate(month, day, year) == true)
{
cout << "The date is valid!" << endl;
}
else
{
cout << "Sorry, that date is invalid." << endl;
}
cin >> Date2;
if (Date2.checkDate(month, day, year) == true)
{
cout << "The date is valid!";
}
else
{
cout << "Sorry, that date is invalid. Please try again.";
}
cout << Date1;
cout << Date2;
bool Date::checkDate(int m, int d, int y)
{
cout << "Now validating the date input." << endl;
if (! (1<= m && m<=12) )
returnfalse;
if (! (1<= d && d<=31) )
returnfalse;
if ( (d==31) && (m==2 || m==4 || m==6 || m==9 || m==11) )
returnfalse;
if ( (d==30) && (m==2) )
returnfalse;
if ( (m==2) && (d==29) && (y%4!=0) )
returnfalse;
if ( (m==2) && (d==29) && (y%400==0) )
returntrue;
if ( (m==2) && (d==29) && (y%100==0) )
returnfalse;
if ( (m==2) && (d==29) && (y%4==0) )
returntrue;
}
Whenever the program reaches the point where it's validating, it keeps telling me my 'month' 'year' and 'day' variables are being used without being initialized, and the program keeps saying the date is invalid whether or not it is valid. I know this validator works, because I've used it in a previous version of this class, and it operated perfectly. What I'm doing here really isn't much different: it's still validating three ints, except they're stored in a date object now.
I'd appreciate any light that could be shed on what I'm not doing right here. Thanks very much.
Also, it definitely would have to be validated by a member function, because those three ints are being held in a member function. That is, they're in Date1 and Date2 because I have to use an overloaded >> operator, which looks like this:
So checkDate() has to be a member function, as far as I know. And the variables not being initialized doesn't explain why it keeps sending me to the 'else' statement and telling me the date is false, when it's not.
I actually have a constructor that initializes the variables until they're given new values by the user.
I think you missed the point. You are passing in the month, day, and year arguments to checkDate, and you are passing in variables that were not initialized. You are not using the class variables that were initialized in the constructor.
cin >> Date1;
if (Date1.checkDate(Date1))
{
cout << "The date is valid!" << endl;
}
else
{
cout << "Sorry, that date is invalid." << endl;
}
cin >> Date2;
if (Date2.checkDate(Date2))
{
cout << "The date is valid!";
}
else
{
cout << "Sorry, that date is invalid. Please try again.";
}
I feel silly for not thinking of that earlier. Thanks again for the help, guys.
In any case, I see now that I wasn't working with the month day and year declared in main, but with the month day and year that are stored in my Date objects, which is what I had to check.
Programming logic has been sort of hard for me to wrap my head around--even more so now that I've switched from procedural to OOD.
I'm hoping I won't have to resort to asking yet another silly question on this forum for a good while, and I'm going to try my best to write the rest of this program on my own.