assuming these are unsigned types,
if( month && (month%13 == month) ) //note the +1, so that the mod allows 0-12, right?
you can use a similar check for whatever else you want to test. You can check the months in bulk groups, is it feb, else is it one of the 30s, else its a 31
you can also probably try to put the value into one of the date formatted objects and see if it is valid, I can't remember but its probable that you can catch an error that way.
class Date
{
public:
Date( int = 1, int = 1, int = 1900 ); // default constructor
void print() const; // print date in month/day/year format
~Date(); // provided to confirm destruction order
private:
int month; // 1-12 (January-December)
int day; // 1-31 based on month
int year; // any year
// utility function to check if day is proper for month and year
int checkDay( int ) const;
};
#include <iostream>
using std::cout;
using std::endl;
#include "Date.h" // include Date class definition
// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
Date::Date( int mn, int dy, int yr )
{
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else
{
month = 1; // invalid month set to 1
cout << "Invalid month (" << mn << ") set to 1.\n";
} // end else
year = yr; // could validate yr
day = checkDay( dy ); // validate the day
// output Date object to show when its constructor is called
cout << "Date object constructor for date ";
print();
cout << endl;
} // end Date constructor
// print Date object in form month/day/year
void Date::print() const
{
cout << month << '/' << day << '/' << year;
} // end function print
// output Date object to show when its destructor is called
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
} // end ~Date destructor
// utility function to confirm proper day value based on
// month and year; handles leap years, too
int Date::checkDay( int testDay ) const
{
staticconstint daysPerMonth[ 13 ] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// determine whether testDay is valid for specified month
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
// February 29 check for leap year
if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Invalid day (" << testDay << ") set to 1.\n";
return 1; // leave object in consistent state if bad value
} // end function checkDay
if(month.ok()) //this is fairly new, and awesome.
…
this is what I was saying above, but I forgot where to find the function and had to look for a bit.
you will still have to work a little on day.ok() as it just checks to 31, and isn't aware of what month it is. looks like what he needs is month_day.ok () ? I think?