--- In [email protected], Vishal Rakhecha <[EMAIL PROTECTED]> wrote: > > hello this is my first mail to this program > i have a problem in my project > i want to validate date weather given date is valid or not > means i want to check date is in right formate > & i am storing date in adate structure > i also want to check if i m not giving any date & press enter > so it will give error > i m working in turboc3.......... > plz try to solve & or give the logic;
Hi, the logic might look like this (assuming you are working with the Gregorian Calendar which is widely adopted around quite large parts of the world but may not be used in your environment, but it's the only modern scheme that I know well enough to explain): 1) If you ask for some particular format (such as "DD.MM.YYYY"), you should give the user some note about this requirement. 2) Let the user input some string (using "fgets()"). 3) As you have required the format "DD.MM.YYYY", you will have to check that: - the string is exactly ten characters long, - characters at positions #2 and #5 are dots '.' , - that all other characters are digits (using "isdigit()"), - that the number at positions 3 and 4 is in the range 1-12, - that the number at positions 6-9 is in the range 1583-9999 (because complete checks for all historic dates in the Julian Calendar is heavy work and makes the checks far more complex), - that the number at positions 0 and 1 is in the range allowed for the given month in the given year. If any of these conditions are not met, the date is invalid, and you should have the user repeat the input. One additional note: You might wonder how to check whether the day number is correct. For this task, please keep in mind that: - January, March, May, July, August, October, and December always have 31 days, - April, June, September, and November always have 30 days, - and that February has 28 days except in leap years. A leap year in the Gregorian Calendar is a year which is: - evenly dividable by 4 and - not evenly dividable by 100. One exception to the second role is important: every year hundred which is evenly dividable by 400 IS a leap year by definition. Some examples: 2006 was no leap year; 2006 % 4 == 2, so not evenly dividable by 4. The first rule above applies here and says you that 2006 was no leap year. 2008 will be a leap year; 2008 % 4 == 0, and 2008 % 100 == 8 (!= 0). Both rules mentioned above apply and tell you this will be a leap year. 2100 will not be a leap year: 2100 % 4 == 0, but 2100 % 100 == 0 and 2100 % 400 != 0, so the second rule above applies but its exception does not. 2000 was a leap year, and 2400 will be the next yearhundred which is a leap year: 2000 % 4 == 0, 2000 % 100 == 0 but 2000 % 400 == 0 as well. The same applies for 2400. So far so clear? If not, please ask again. Regards, Nico
