Thread: date messed up

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    date messed up

    I am trying to get this to work, but I keep getting the same bad result. here is the code
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main() {
            struct tm date;
            time_t dt;
    
            printf("Enter a day of the month.\n");
            scanf("%i", &date.tm_mday);
            printf("Enter the month.\n");
            scanf("%i", &date.tm_mon);
            printf("Enter the year for 1900.\n");
            scanf("%i", &date.tm_year);
            printf("Enter the day of the weak.\n");
            scanf("%i", &date.tm_wday);
            dt = mktime(&date);
            printf("%s.\n", ctime(&dt));
            return 0;
    }
    here is the output.
    bash-2.05b$ ./date
    Enter a day of the month.
    03
    Enter the month.
    07
    Enter the year for 1900.
    104
    Enter the day of the weak.
    01
    Wed Dec 31 17:59:59 1969
    .

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I don't see the problem exactly...
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main() {
    	struct tm date = {0,0,0,0,0,0,0,0,0};
            time_t dt;
    		
            printf("Enter a day of the month.\n");
            scanf("%i", &date.tm_mday);
            printf("Enter the month.\n");
            scanf("%i", &date.tm_mon);
            printf("Enter the year for 1900.\n");
            scanf("%i", &date.tm_year);
            printf("Enter the day of the weak.\n");
            scanf("%i", &date.tm_wday);
            dt = mktime(&date);
            printf("%s.\n", ctime(&dt));
            return 0;
    }
    Try this...

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    thanx, just had to initialize everything to zero, instead of your way, just made it global because I remembered from my c book that anything external is automatically initialized to zero.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    a) don't make it global, globals are generally messy
    b) don't rely that all globals will be initialized to 0. Although they will be, don't rely on it. Relying on globals for 'easy use' is bad.

    The reason it looked so messy is because the variables were uninitialized (usually to 0xCCCCCC) and that was the reason for the messy stuff. Don't be lazy and just take the extra little step of initializing them yourself.

    -LC
    Last edited by Lynux-Penguin; 08-05-2003 at 02:11 AM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM