C- Challenge mini_calendar

Hello there,
i´m sticking quite a time at this challenge: https://www.codecademy.com/courses/learn-c/projects/mini-calendar-c

i do not get errors, but the output at the case the year is a leap-year if get an “off-by-one” - error.
Means i get one day further for example:

input: “2 28 2020 2” → should output “3 1 2020”.

But i am getting on and on the output: “3 2 2020”.

If the case is a non- leap year like 2019 etc. it outputs the right day

I don´t know any more places in the code where i can look for the mistake.

If you could help me out with this i would appreciate it!

very thankful

-Kevin

/// MonthArray int days_in_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //// day addition void add_days_to_date(int *mm, int *dd, int *yy, int days_left_to_add) { while (days_left_to_add > 0) { int days_left_in_month = days_in_month[*mm] - *dd; if ( days_in_month[2] == *mm && is_leap_year(*yy) == true) { days_left_in_month++; } if (days_left_to_add > days_left_in_month) { days_left_to_add -= days_left_in_month + 1; *dd = 1; if (*mm == 12) { *mm = 1; *yy = *yy + 1; } else { *mm = *mm + 1; } } else { *dd = *dd + days_left_to_add; days_left_to_add = 0; } } } // end add_days_to_date //// USER input USER output int main() { int mm = 0; int dd = 0; int yy = 0; int days_left_to_add = 0; printf("Please enter a date between the years 1800 and 10000 in the format mm dd yy and provide the number of days to add to this date:\n"); scanf("%i%i%i%i", &mm, &dd, &yy, &days_left_to_add); add_days_to_date(&mm, &dd, &yy, days_left_to_add); printf("%i %i %i\n", mm, dd, yy); } // end main

Hello @bit8098660939, welcome to the forums! Is that all the code? It seems to be missing a declaration for the is_leap_year() function. Regardless, this line seems to be dodgy:

if ( days_in_month[2] == *mm && !(*yy % 4)) {
     ^^^^^^^^^^^^^^^^

You’re checking to see if the input value for month is equal to the number of days in February. But, the input for Feb would be 2, so you’re actually checking to see if 28 == 2. Meaning no matter whether it’s a leap year or not, (and if the user has selected Feb), that if block will never run.

Thank you very much, i didn´t noticed that it searches for eqality to 2. Thanks.
yeah i noticed i copied it without the is_leap_year( ) function.

Thanks alot

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.