You are making a couple mistakes that is confusing you.
There are, at any one time,
more than one variable named "month":
- Your program as a variable named "month" in main(), which we might call "main.month".
- It has another variable named month declared in your Date object, which is itself declared in main, so we might call it "main.userInput.month".
- There are other variables declared as arguments to some of the functions, such as "userInput.CheckDate(month,...)".
(The same is true for "day" and "year".)
The error is that you are confusing all these things. Sometimes you use main's "month". Sometimes you use userInput's "month". Sometimes you use arguments (and sometimes you ignore them). This is where you are failing.
(Am I correct in assuming that no matter what date you enter it always validates as OK?)
______________________________
Before going any further, let's fix a couple of errors with the class's design.
The "Date" class itself is supposed to handle a month, day, and year. All other instances of variables named "month", "day", and "year" should only be temporary -- we are only interested in a Date's month, day, and year.
The next thing is that getX() and setX() methods are not supposed to be doing any I/O -- all they should do is get or set a Date's month, day, and year values. (I don't know how much detail your professor has taken you to yet, but the setX() method should also make sure that the Date does not have invalid values for month, day, and year.)
So, let's rewrite that header a little bit:
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 33
|
#ifndef Date_H
#define Date_H
#include <iostream>
/*********Class Date Specification**********/
class Date
{
private:
int month;
int day;
int year;
public:
Date();
Date(int m, int d, int y); // Make sure to give the arguments NAMES
//mutator methods == methods that CHANGE the object
void setDate(int m, int d, int y);
//accessor methods == methods that OBSERVE the object
void getDate(int& m, int& d, int& y) const; // WHAT ARE the object's current m,d,y values?
//I/O methods == methods that perform I/O
void showDate() const; // this prints to the console, apparenty
// (You could, conceivably, add a method that reads a date from the console -- something like readDate();.)
//information and validation functions - these work on ARGUMENTS and do nothing else
bool isLeap(int y) const; // is the ARGUMENT y a leap year?
bool CheckDate(int m, int d, int y) const; // is the ARGUMENT date m,d,y a valid date?
};
#endif
|
(The "const" stuff says that a specific method does not change a Date object. Only the mutator methods do that.)
Notice also how
getDate() should
not do any I/O? All it does is allow you to find out what the Date's current month, day, and year are. For example, I can find out what the default month is this way:
1 2 3 4
|
Date default_date;
int month,d,y;
default_date.getDate(month,d,y);
cout << "The default month is: " << month << endl;
|
(It should say "1" because the default constructor sets the date to 1,1,2000 on line 9 of "Date.cpp".)
If I want to have the user input a date, I currently need to do it myself:
1 2 3 4 5
|
Date user_date;
int mo,day,yr;
cout << "Enter the date as, for example, 2 28 2003: ";
cin >> mo >> day >> yr;
user_date.setDate(mo,day,yr);
|
______________________________
Remember that I said that the date should be automatically validated? This should be done in two spots, both of which are when someone sets the Date's month, day, and year:
- the non-default constructor (the one taking arguments)
- the setDate() method
- the readDate() method if you create it
If the new date is invalid then the date should not be changed. For example:
1 2 3 4 5 6 7 8 9 10 11
|
int main()
{
Date fooey( 72, -4, 4013 );
fooey.showDate();
Date good( 7, 4, 2012 );
good.showDate();
good.setDate( -7, 512, 32 );
good.showDate();
}
|
% ./a.out
1/1/2000
7/4/2012
7/4/2012
% |
Notice that the first date that printed was the
default date value -- since we tried to create a date with an invalid value. The date constructor refused to let us do that.
The second date was OK, so the constructor let us do that without any problems.
We then tried to set the good date to something bad, but the setDate() method didn't let us do that, so the next time we print it we see that the date was not allowed to be changed.
Here is how the constructor makes sure that it is OK:
1 2 3 4 5 6 7 8 9
|
Date::Date(int m, int d, int y)
{
if (CheckDate(m,d,y))
{
month = m;
day = d;
year = y;
}
}
|
The setDate() function should work very much the same.
The getDate() function has reference arguments:
1 2 3 4 5
|
void getDate(int& m, int& d, int& y) const
{
m = month;
...
}
|
Make sense?
______________________________
You did not post your IsLeapYear() method, but I presume it uses the standard formula?
Also, I am not sure what is wrong with dates before 1850...
Hope this helps.