I am struggling to write the if statement to find if a day is valid in a specific month. For example, April has 30 days so if the user inputs month as 4 and as day 30 , it will set the day to 30 and month to 4. I have include part of the code only that i need help with. Thanks for any help!
1 2 3 4 5 6 7 8 9 10 11 12
class DayOfYear
{
public:
DayOfYear(int m, int d)
{
if (month <= 12) // here is the if statement
{
month = m;
}
}
First, you need to find the maximum number of days that month has, which is easy just by using switch case (don't forget to take leap years into account). After that, check if the passed day is equal or less than the maximun number of days that we've just calculated.
typedefunsignedchar byte;
byte DaysPerMonth[] = {0, 31, 28,31,30,31,30, 31,31,30,31,30,31};
if the year is a leap year then you set DaysPerMonth[2] to 29.
A valid day is between 1 and DaysPerMonth[NumberOfMonth]