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