How can I verify if a date is correct?
example: 31 02 1999 is not correct, Febrery has only 28 or 29 days.
I would like apreciate some help, i've tryed but i can't do it
How can I verify if a date is correct?
example: 31 02 1999 is not correct, Febrery has only 28 or 29 days.
I would like apreciate some help, i've tryed but i can't do it
this is no function in the standard that does so, so you need to tell us what OS/compiler you use.
So what did you try?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i use dev c++ with win xp. i've tried the if function but i can't get it right
I guess you need to write a function that validates the date, examples of things to check:
day <= 31 when month == 1
day <= 29 when month == 2
first figure out all of the conditions.
write an if/elseif/else (or a switch maybe?) construct that validates the date depending on the above conditions, if you still can't get it to work, show us your code, then we will help.
> i use dev c++ with win xp. i've tried the if function but i can't get it right
Yeah, and now what code did you try?
In order to validate the day, you need to know the number of days in a month
In order to know the number of days in february, you need to validate the year (and work out the leap year rules)
So it goes
year
month
day
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
And if isn't a function - for some reason that is a real pet peeve of mine.
Do not make direct eye contact with me.
Code:int daysinmonth(int month,int year){ int days[]={31,0,31,30,31,30,31,31,30,31,30,31}; days[1]=((year%4==0 && year%100 !=0) || year%400 ==0)?29:28; return days[month-1]; } int validatedate(int day,int month,int year){ if(day>= 1 && day <=daysinmonth(month,year)) return 1; return 0; }
tks. now i need implant that in something like that:
the program should ask the date until a correct one is inputed. should i do it with a
do
while
???
sorry but i need to do a scholl work and i'm kind of lost
Simple way to force the user to enter a correct date would be
Where checkdate() is the code above. It would return 0 or 1 depending if its false or true respectivally. You can modify the loop to get the information in the manner you want but the basic idea would stay the same.Code:do { printf("Enter date: "); fgets(datebuffer, sizeof(datebuffer), stdin); }while ( checkdate(datebuffer) );
i've done like this:
Code:int diaprov=0,mesprov=0,anoprov=0; int verificardata() { int datacorrecta=0; int diasmeses[]={31,28,31,30,31,30,31,31,30,31,30,31}; if (anoprov%4==0) { diasmeses[1]=29; } if (mesprov<1 || mesprov>12) { datacorrecta=0;} else { if (mesprov>=1 && mesprov<=12) { if (diaprov>diasmeses[mesprov-1]) { datacorrecta=0;} else {if (diaprov<=diasmeses[mesprov-1] && diaprov>=1) { datacorrecta=1;} } return datacorrecta; } } } int main() { printf("\n\nData de nascimento (dia mes ano): "); do {scanf(" %i %i %i", &diaprov, &mesprov, &anoprov);} while (verificardata()!=1); contacto[i].dian=diaprov; contacto[i].mesn=mesprov; contacto[i].anon=anoprov; }
COULD YOU TELL ME WHAT IS WRONG. SORRY BOUT THE LANGUAGE BUT I'M PORTUGUESE
Just use shiju's code, he got that right. When you want to validate the date use the validatedate function -
Boom, done.Code:if (!validatedate(day month year)) printf("Bad date, take care of this");
(edit) Hell, I'm bored. I'll complete the code.
Code:#include <stdio.h> int daysinmonth(int month, int year); int validatedate(int day, int month, int year); int main() { int day, month, year; /* Toss a good date at it. */ day = 11; month = 9; /* September */ year = 2001; if (!validatedate(day, month, year)) printf("Bad date! Bad dog! No biscuit for you!\n"); else printf("Works. W00t.\n"); /* Exact same code, toss a bad date at it. */ day = 31; month = 9; /* September has 30 days, NOT 31! */ year = 2004; if (!validatedate(day, month, year)) printf("Bad date! Bad dog! No biscuit for you!\n"); else printf("Works. W00t.\n"); return 0; } int daysinmonth(int month,int year){ int days[]={31,0,31,30,31,30,31,31,30,31,30,31}; days[1]=((year%4==0 && year%100 !=0) || year%400 ==0)?29:28; return days[month-1]; } int validatedate(int day,int month,int year){ if(day>= 1 && day <=daysinmonth(month,year)) return 1; return 0; }
Last edited by JLPence; 01-13-2004 at 01:11 PM.