I have the following class but can't seem to figure out how to validate the inputs.
//Define Default Constructor
DateC::DateC()
{
day = 1;
month = 1;
year = 2000;
}
//Define Constructor
DateC::DateC(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
//Define GetDay
int DateC:: getday()
{
return day;
}
//Define GetMonth
int DateC:: getmonth()
{
return month;
}
//Define GetYear
int DateC:: getyear()
{
return year;
}
I fleshed out your code and showed you one way to validate the month field. You can reuse the validateMonth function if you add a setMonth() function to your class. What you do with the field when validation fails is totally up to you.
this is the kind of thing you are going to do a lot of, and you may want some sort of tool you can use to validate common things (ints and doubles within ranges for examples). I keep mine as stand alone function / procedural design but you can bundle into an object if you feel a need. Putting them into the class, though, limits re-use, so consider moving it out.
doug4's validateMonth() is a good start, but you'll need more. I suggest a general validate() method that validates the entire date. After all, some days are only valid on some months, or some months in some years (think February 29).