Trouble with a date validator

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;


And here's the checkDate validator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool Date::checkDate(int m, int d, int y)
{

	cout << "Now validating the date input." << endl;
	
  if (! (1<= m && m<=12) )
     return false;
  if (! (1<= d && d<=31) )
     return false;
  if ( (d==31) && (m==2 || m==4 || m==6 || m==9 || m==11) )
     return false;
  if ( (d==30) && (m==2) )
     return false;
  if ( (m==2) && (d==29) && (y%4!=0) )
     return false;
  if ( (m==2) && (d==29) && (y%400==0) )
     return true;
  if ( (m==2) && (d==29) && (y%100==0) )
     return false;
  if ( (m==2) && (d==29) && (y%4==0)  )
     return true;
	
	
}


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.
it keeps telling me my 'month' 'year' and 'day' variables are being used without being initialized
The compiler is right. Where are those variables initialized?

Since checkDate() doesn't use member variables of the class Date it can be a static function or don't need to be a member.
I actually have a constructor that initializes the variables until they're given new values by the user.

1
2
3
4
5
6
Date::Date(int m, int d, int y)
{
	month = m;
	year = y;
	day = d;
}


Or at least I thought it was doing that, until this problem came up.
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:
1
2
3
4
5
6
7
8
9
10
11
istream &operator >> (istream &strm, Date &obj)
{
	cout << "Month: ";
	strm >> obj.month;
	cout << "Day: ";
	strm >> obj.day;
	cout << "Year: ";
	strm >> obj.year;

	return strm;
}


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.
All right, I figured out what was wrong. I was still thinking in terms of procedural programming rather than object-oriented programming.

My new validator function call looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.
The call should look like:

1
2
3
4
5
if ( Date2.checkDate() )
{
    cout << "The date is valid!" ;
}
...
Or that works too! Haha

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.
Topic archived. No new replies allowed.