Mini Calendar not producing any output

Hello! I am on the “Mini Calendar” challenge in the C skill path. It seems all fine, I get the code running, there are no compile errors. But when I enter the date and days to add, nothing appears. Can you see what is wrong? maybe something with the scanf() or print() at the end (in the the main function.) ? Link to task “Mini Calendar”
Thank you for giving me a clue!

#include <stdio.h>
#include <stdbool.h>
bool is_leap_year(int year) {
if (year % 4 != 0) {
  return false;
} else if (year % 100 != 0) {
  return true;
} else if (year % 400 != 0) {
  return false;
} else { 
  return true;
  } 
  /* --- ALTERNATIVE WAY WITH AND OR OPERATORS ------
  if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
    return true;
    } else {
      return false;
      }*/

}

int days_in_month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

/* ADD DAYS TO DATE */
void add_days_to_date (int* dd, int* mm, int* yy, int days_left_to_add) {
int days_left_in_month;
while (days_left_in_month > 0) {
  days_left_in_month = days_in_month[*mm] - *dd;
  if (*mm == 2 && is_leap_year(*yy) == true )   {
     days_left_in_month++;
    }
  }
  if (days_left_to_add > days_left_in_month) { // if more days left than can be added in a month
     days_left_to_add -= days_left_in_month + 1;
     // jump to the first day of the next month 
     *dd = 1;
     // we are on the first day of the next month, but what is the next month?
     if (*mm == 12) { // if last month was December
      *mm = 1; // next month is January
      *yy += 1; // and we are in a new year so increase by 1
     } else *mm += 1; // if some other month, just increase by 1
     } else {  // if all days can be added in this month
     *dd = *dd + days_left_to_add; // simply add the days
     days_left_to_add = 0; // no more days to add!
     }
  }


/* MAIN FUNCTION*/
int main() {
int dd;
int mm;
int yy;
int days_left_to_add;

printf("Please enter a date between the years 1800 and 10000 and provide the number of days to add to this date. Use the following format for input dd mm yy nn (separated by space)\n");
scanf("%d%d%d%d", &dd, &mm, &yy, &days_left_to_add);


add_days_to_date(&dd, &mm, &yy, days_left_to_add);

printf("%d %d %d\n", dd, mm, yy);


/*int year;
printf("Enter a year between 1800 and 10000: ");
scanf("%d", &year);
is_leap_year(year);

if (is_leap_year(year) == true) {
  printf("This is a Leap Year\n");
} else {
 printf("This is Not a Leap Year\n");
}*/

}
int days_left_in_month;
while (days_left_in_month > 0) 

Does this code go into the while loop with this statement?
Also in C, you should initialize integers explicitly if you need them to be 0. Implicit initialization is undefined (you may get 0 with some compilers, and some junk value with others).

Also, since you’re learning C, setting up use of a debugger is so so so so so important. Look into gdb if you’re feeling brave, or something simpler if through an IDE like vs code etc.

Thanks! That helped. Now it’s compiling correctly and I get an output, though not the right date, there must be still something wrong with the calculations within the while loop, will look into that.

1 Like

isnt the while loop supposed to pass thru the “days_left_to_add” variable from the function?