Trouble with Mini-Calendar Project in C

This is based of the LEARN C Mini-Calendar Project
=====================x==========================

In my program, I am asking the user to input a date(in just integers ie 12 31 2019 367) and the number of days they add to it. In one of my functions, this is precisely what I am doing.

The user inputs 12 31 2019 367, and the program is meant to print 1 1 2021, but instead prints 1 1 2020(a year behind)…

What I did(Sorry for a lot of code, I tried to keep it simple and clean):

int days_in_month[] = {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_in_month > 0)
  {
    days_left_in_month = days_in_month[*mm] - *dd;
 //   days_left_in_month = days_in_month[*mm] - *dd;
    if (days_in_month[2] && is_leap_year(*yy) == true)
    {
      days_left_in_month++;
    }
   } // end while
   printf("after while\n");
    if(days_left_to_add > days_left_in_month)
    {
      days_left_to_add -= days_left_in_month;
      *dd = 1;
      if(*mm == 12)
      {
        *mm = 1;
        (*yy)++;
      }
      else
      {
        (*mm)++;
      }
    }
    else
    {
      *dd += days_left_to_add;
      days_left_to_add = 0;
    }
}
int main()
{
  int mm, dd, yy, days_left_to_add;
  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("%d %d %d %d", &mm, &dd, &yy, &days_left_to_add);
 // printf("\nREAD\n");
  //These are pointers, so they have to be at certain location i.e. int* mm = &mm
  add_days_to_date(&mm, &dd, &yy, days_left_to_add);
  printf("%d %d %d\n", mm, dd, yy);
}

What I got after inputs:

Inputs: 12 31 2019 367

Output: 1 1 2020(meant to be 1 1 2021)

Thank you in advance and for your time and patience…

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