validating a date month and year format

This is a discussion on validating a date month and year format within the C Programming forums, part of the General Programming Boards category; Hi guys im having trouble with validating user input for a date formal as dd/mm/yyyy I am using sscanf() function ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    validating a date month and year format

    Hi guys im having trouble with validating user input for a date formal as

    dd/mm/yyyy

    I am using sscanf() function to read in the values but as soon as it hits the sscanf() function it reads the integer values but does not go and execute the next lines so it can validate the day,month, and years ranges using the if statements. Any idea's?

    Code:
    if ( sscanf( buff, "%d/%d/%d", &date, &month, &year ) == 3 ) 
      {
     
        if((date <=0) && (date>31))
        {
             printf("please enter the date again!");
        }
    
        if(month <0 && month>12)
        {
             
             printf("enter the months again please!");
        }
    
        if(year <1970 && year>2005)
        {
    
           printf("please enter the year again!");
        }
    
      }

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i dont know much about that cause im a c++ programmer mainly,
    but no one has put a answer so ill just take a guess.

    %d i thought that was for double's?

    maybe %i would make a difference?


    if nto maybe you could try


    %i%c%i%c%i


    where c would be for character and take the / off,
    but like i said i dont know how it works, id imagine
    it just read to the next int if you dont put the %c,
    but it just my guess.

    and just out of curiosity what is the / for in the %d/%d/%d
    i could be wrong but i dont think the / are suppose to be there?
    Last edited by ILoveVectors; 08-15-2005 at 09:13 PM.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    validation

    thanks for that the / is just so that when the user inputs the date
    they input the integer and a slash then another integer and a slash

    eg 00/00/0000

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by bazzano
    I am using sscanf() function to read in the values but as soon as it hits the sscanf() function it reads the integer values but does not go and execute the next lines so it can validate the day,month, and years ranges using the if statements. Any idea's?
    Are you sure it's not getting there?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char buff[] = "16/8/2005\n";
       int date, month, year;
       if ( sscanf( buff, "%d/%d/%d", &date, &month, &year ) == 3 )
       {
          printf("date = %d, month = %d, year = %d\n", date, month, year);
       }
       return 0;
    }
    
    /* my output
    date = 16, month = 8, year = 2005
    */
    Quote Originally Posted by ILoveVectors
    %d i thought that was for double's?
    No, %d is not for doubles -- it is for ints. For *scanf, %d specifies base 10; with %i the expected format "is the same as expected for the subject sequence of the strtol function with the value 0 for the base argument." (A double uses %lf with *scanf.)
    Last edited by Dave_Sinkula; 08-16-2005 at 07:47 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Display day, month, year in different format
    By melodious in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2007, 01:14 AM
  2. Getting the Following Date
    By BB18 in forum C Programming
    Replies: 2
    Last Post: 10-10-2004, 12:39 PM
  3. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 02:36 PM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM

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