casting the system exstracted date into seperate ints

This is a discussion on casting the system exstracted date into seperate ints within the C Programming forums, part of the General Programming Boards category; Hey guys im writing a function to exstract the user date and then ask user input for there bithday to ...

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

    casting the system exstracted date into seperate ints

    Hey guys im writing a function to exstract the user date and then ask user input for there bithday to calculate there age. I have tokenized the current date but how would i put those 3 strings into 3 ints by casting. My casting does not work.. for example user enters 12/12/1984
    the strtok tokenizers it into 12 12 1984 as a string. But how would i put them into seperate integers called dayr monthr and yearr? This is an ansi c program!

    Code:
    void ageCalculator()
    {
     char buff[BUFSIZ];
     struct tm t;
     time_t now;
     int m,d,y;
     char input [MAXDATE]; /* buffer size is 12 */
     int valid = 0;
     int date[3]; /* 0 = day 1= month 2=year*/
     
     char *day, *month, *year;
     int dayr, monthr, yearr;
     /* declaration of variables */
    
     time(&now); 
     /* put now into time */
    
     t = *localtime(&now); 
     /* get the current date */
    
     strftime(buff, sizeof(buff), "%d//%m//%y", &t); 
     /* format the date*/
      
    
     /* --- printing the date ---- */
      printf("The current date is %s\n",buff);
    
      printf("\nPlease Enter your age in dd/mm/yyyy format:");
    
       fgets(input, MAXDATE, stdin);
    
      do
      {
    
       if ( fgets ( input, sizeof input, stdin ) != NULL ) 
       {
         valid = 0;
           
       }     
    
        if ( sscanf ( input, "%d/%d/%d", &m, &d, &y ) == 3 )
         {
          if ( is_valid ( m, d, y ) )
          {
            printf ( "\n\n%d/%d/%d is a valid date\n", m, d, y );
            valid = 1;
          }
        }
          else
          {
            printf ("\nInvalid date please enter in dd/mm/yyyy format:");
            valid = 0;
          }
       }
       while(!valid);    
       
       day = strtok(input, "/");
       if (day) printf("%s\n", day); // tokenizing
    
        month = strtok(NULL, "/"); // casting
        if (month)   printf("%s\n", month);
    
       year = strtok(NULL, "/");
       if (year) printf("%s\n", year);   
       loopyear();
    
       // this tokenizers current date and attempts to cast
       dayr = strtok(buff, "/");
       if (dayr) printf("%s\n", dayr);
       dayr = atoi(&t);
       
       monthr = strtok(NULL, "/");
       if (monthr) printf("%s\n", monthr);
       monthr = atoi(&t);
    
       yearr = strtok(NULL, "/");
       if (yearr) strtok(NULL, "/"); printf("%s\n",yearr);
       yearr = atoi(&t);   
     }
    
    
    int is_valid ( int m, int d, int y )
    {
      static const int  mday[] =
      {
        0,31,28,31,30,31,30,31,31,30,31,30,31
      };
    
      if ( m <= 0 || m > 12 )
        return 0;
      else if ( d <= 0 || d > mday[m] )
        return 0;
      else if ( y < 1900 || y > 3000 ) /* Arbitrary choices */
        return 0;
    
      return 1;
    }
    
    int loopyear()
    {
     int days = 0;
     int leapdays;
     int nonleapdays;
     int year;
    
     leapdays = year % 4;
     nonleapdays = year % 100;
    
     days = leapdays - nonleapdays;
     printf("\n%d number of days",days);
     return days;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,339
    So what was wrong with my answer here http://forums.devshed.com/t284906/s.html
    posted over 12 hours before you reposted the same thing here?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to ouput system date and time?
    By toaster in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2002, 10:58 PM
  2. reading the system date
    By Alicia in forum C++ Programming
    Replies: 5
    Last Post: 02-13-2002, 02:18 PM
  3. Help! I need to know how to use the system date!
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2001, 10:20 PM
  4. how to pull system time, date, etc from <ctime>
    By Matt in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2001, 09:08 AM
  5. System Date
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2001, 06:03 PM

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