Trouble with Mini Calendar Project

Hello there,
I’m having a bit of an issue with my code. It runs smoothly, but for some reason adds an extra 2 days more than necessary. I tried calculating 8th of April and instead of the 3rd of June I got the 5th. Can you help me find my mistake? Thank you!

#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 true;
  }
  else {
    return false;
  }
}
int days_in_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

void add_days_to_date(int* mm, int* dd, int* yy, int days_left_to_add){
int days_left_in_month; 
while (days_left_to_add>0){
    days_left_in_month= days_in_month[*mm]-*dd;
    if (*mm==2 && is_leap_year(*yy)){
    days_left_in_month+=1;	
  }

  if (days_left_to_add>=days_left_in_month){
   days_left_to_add-=days_left_in_month;
    days_left_to_add-1;
    *dd=1;
    if (*mm==12){
      *mm=1;
      *yy+=1;  
                }
	else{
    *mm+=1;
        }
                                          }
  else{
    *dd = *dd + days_left_to_add; 
    days_left_to_add = 0; 
       }
  }

  }

int main() {

int mm, dd, yy, days_left_to_add;

printf("mm dd yy num:\n");
scanf("%d%d%d%d", &mm, &dd, &yy, &days_left_to_add);
add_days_to_date(&mm, &dd, &yy, days_left_to_add);
printf("%d %d %d\n", mm, dd, yy);
/*int year;

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

if (is_leap_year(year)==true)
  printf("leap year");
else 
  printf("not a leap year");*/




}

Use a debugger!
Check your assumptions in key places with it.

Getting used to using a debugger with C is invaluable.
https://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)Debugging.html

2 Likes

Thank you for the tip!