I can't have the time_in printed out right, can you please show where my code went wrong? Thank you and hope you have a great day!
Input:
55 17:30 11-06-2023 07:15 15-07-2023, the first number is the price for renting a bike (not very neccessary), and numbers after that are the rent time and the return time in the form: HH:MM DD-MM-YY, print out the rent time and the return time in the formlong intusingmktimeand in the standard form (e.gSat Jul 15 07:15:01 2023) usingctime
Here's my code:
#include <stdio.h>
#include <time.h>
int main(){
int price;
scanf("%d", &price);
struct tm input_time;
struct tm output_time;
time_t time_in, time_out;
scanf("%d:%d %d-%d-%d", &input_time.tm_hour, &input_time.tm_min, &input_time.tm_mday, &input_time.tm_mon, &input_time.tm_year);
scanf("%d:%d %d-%d-%d", &output_time.tm_hour, &output_time.tm_min, &output_time.tm_mday, &output_time.tm_mon, &output_time.tm_year);
input_time.tm_mon -= 1;
output_time.tm_mon -= 1;
input_time.tm_year -= 1900;
output_time.tm_year -= 1900;
// printing out every component to check if i failed to input them
printf("%d\n", price);
printf("%ld:", input_time.tm_hour );
printf("%ld ", input_time.tm_min );
printf("%ld-", input_time.tm_mday );
printf("%ld-", input_time.tm_mon );
printf("%ld\n", input_time.tm_year );
printf("%ld:", output_time.tm_hour );
printf("%ld ", output_time.tm_min );
printf("%ld-", output_time.tm_mday );
printf("%ld-", output_time.tm_mon );
printf("%ld\n", output_time.tm_year );
//using mktime
time_in = mktime(&input_time);
time_out = mktime(&output_time);
// print out the time in seconds form
printf("%ld\n", time_in);
printf("%ld\n", time_out);
// print out the time in the 'Sat Jul 15 07:15:01 2023' form
printf(ctime(&time_in));
printf(ctime(&time_out));
}
Here're my output, i got the return time printed right, but the rent time is all messed up, i keeps changing everytime i run the code
1st run:
550000
17:30 11-5-123
7:15 15-6-123
1592440040
1689380101
Thu Jun 18 07:27:20 2020
Sat Jul 15 07:15:01 2023
2nd run:
550000
17:30 11-5-123
7:15 15-6-123
-2085243656
1689380101
Mon Jan 09 19:07:20 2040
Sat Jul 15 07:15:01 2023
You have:
These are on the stack. So, they are uninitialized. The parts of the structs that you don't explicitly set can have random values.
This is because they take on whatever values just "happen" to be there from earlier parts of the execution of the program.
This is UB (undefined behavior).
To fix this, initialize all elements to zero:
Note that although you did check some values, some others (e.g.
tm_isdst,tm_sec, etc.) were not initialized. These values are filled in by (e.g.localtime) butmktime[is finicky and] needs some more of them.Also, some
printfused%ldinstead of%d.On my system, the UB always produced consistent results. That's the nature of UB. It can work, work intermittantly, be incorrect, segfault.
So, to simulate the random behavior, I refactored the code to use
randto pre-seed the structs.Here is the input that I used to test:
Here is the "bad" output for 5 runs:
Running the program with
-za few times: