How can I verify if a date is correct?

This is a discussion on How can I verify if a date is correct? within the C Programming forums, part of the General Programming Boards category; How can I verify if a date is correct? example: 31 02 1999 is not correct, Febrery has only 28 ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    21

    Question How can I verify if a date is correct?

    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

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    this is no function in the standard that does so, so you need to tell us what OS/compiler you use.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    21

    i use dev c++ with win xp

    i use dev c++ with win xp. i've tried the if function but i can't get it right

  5. #5
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    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.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    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.

  8. #8
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41
    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;
    }

  9. #9
    Registered User
    Join Date
    Jan 2004
    Posts
    21

    now what?

    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

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Simple way to force the user to enter a correct date would be
    Code:
    do
    {
      printf("Enter date: ");
      fgets(datebuffer, sizeof(datebuffer), stdin);
    }while ( checkdate(datebuffer) );
    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.

  11. #11
    Registered User
    Join Date
    Jan 2004
    Posts
    21

    i've done like this

    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

  12. #12
    Registered User
    Join Date
    Jan 2004
    Posts
    26
    Just use shiju's code, he got that right. When you want to validate the date use the validatedate function -

    Code:
    if (!validatedate(day month year)) printf("Bad date, take care of this");
    Boom, done.

    (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 12:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 07:24 AM
  2. plz help me geeting the date is correct or not
    By vijay85 in forum C Programming
    Replies: 1
    Last Post: 03-20-2009, 08:05 AM
  3. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  5. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21