-
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
.
-
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
-
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.
-
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