Thread: casting error

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

    casting error

    hey guys im trying to seperate the day,month and year from the system date and i have with strtok. But when i try to cast it i get errors what is wrong with the way im tokenizing and casting the exstracted date into ints?




    Code:
    /****************************************************************************
    * This function prompts the user for a 10-character date of birth in
    * dd/mm/yyyy format. The system date is extracted with the assistance of the
    * time.h header file. This function reports how many days old the user is.
    * Dates of birth after the current date are rejected.
    ****************************************************************************/
    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);
    
        month = strtok(NULL, "/");
        if (month)   printf("%s\n", month);
    
       year = strtok(NULL, "/");
       if (year) printf("%s\n", year);   
       loopyear();
    
    
       dayr = strtok(buff, "/");
       if (dayr) printf("%s\n", dayr);
       dayr = atoi(buff);
       
       monthr = strtok(NULL, "/");
       if (monthr) printf("%s\n", monthr);
       monthr = atoi(buff);
    
       yearr = strtok(NULL, "/");
       if (yearr) strtok(NULL, "/"); printf("%s\n",yearr);
       yearr = atoi(buff);   
     }
    
    
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM