Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
C#
private int checkDay( int testDay )
{
      int daysperMonth [] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
       // check if day in rang for month
      if (testDay >0 && testDay <= daysperMonth[month])
         return testDay ;
         // check for leap year

      if (month ==2 && testDay ==24 &&(year %400 ==0 || (year %4 ==0 && year %100!=0) ) )
         return testDay ;
         System.out.printf ("Invalid day (%d) set to 1 ", testDay);
         return 1 ; // maintain object n consistent state
     } // end method checkDay 
Posted
Updated 9-Feb-14 6:09am
v2
Comments
Richard MacCutchan 9-Feb-14 12:19pm    
It is trying to check whether a date is valid or not. But it won't work for February 29th.

It looks at the date information provided and decides if the date is valid.

However, it doesn't work. No month has only 24 days, even in a leap year...
And it returns poor information: a bool valid/invalid check would be a lot more sensible.
 
Share this answer
 
This code is checking if the given day is in the calendar (that is its number is between current month's minimum and maximum number).

The following line
Quote:
if (month ==2 && testDay ==24 &&(year %400 ==0 || (year %4 ==0 && year %100!=0) ) )


Is wrong: it should be
C++
if (month ==2 && testDay ==29 && (year %400 ==0 || (year %4 ==0 && year %100!=0) ) )

It checks if 29th of february is a valid day, that is if current year is a leap year.
You know, a year is leap if it is exactly divisible by 4 but it isn't a century year (e.g. 1900), unless it is also divisible by 400 (e.g. 2000).

By the way, the indentation is misleading.

[update]
As noted by OriginalGriff (I have overlooked it)
Quote:
return 1 ; // maintain object n consistent state

is plain madness: if you detect an invalid input, you have to signal it.
[/update]
 
Share this answer
 
v2
Comments
CHill60 9-Feb-14 12:41pm    
Mea culpa on the indendation - I slapped some pre markers around his code - should have checked it. Sorry.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900