Mini Calendar | C

Hello everyone! Hope you are doing well.

I am encountering some issues with the final project of functions.
It works almost ok, but when I test the final condition which is to add 367 to 12 31 2019 (it should output 1 1 2020) it just doesnt work and i have been staring at the code for hours and just cant get my mind arround the error.

If someone could help me would be so great!

#include <stdio.h>
#include <stdbool.h>

bool is_leap_year(int year){
  // año que NO puede dividirse por 4 = NOT LEAP
  if (year % 4 != 0){
    return false;
    }
    // año que SI puede / 4 NO puede dividirse por 100 = LEAP
  else if (year % 100 != 0){
    return true; }
    // año que SI se divide x 100 pero NO se divide por 400 = NOT LEAP
    else if (year % 400 != 0){
      return false;
      } else return true;     
}

//CALENDAR
// amount of days per month
int days_in_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30};

//
void add_days_to_date(int* mm, int* dd, int* yy, int days_left_to_add){
  // how many more days can be added to the current month?
  while (days_left_to_add > 0){
    int days_left_in_month = days_in_month[*mm] - *dd;
    //if its leap year, february + 1 day
    if (days_in_month[2] && is_leap_year(*yy) == true) {
      days_left_in_month++;
    }
  //if we have more days left to be added than the ones we can fit in one month, move to the next month
  if (days_left_to_add > days_left_in_month) {
      days_left_to_add -= days_left_in_month + 1; //enter new month
      *dd = 1; //make sure its first day of the month
      if (*mm == 12){ //if we are on december move to next year
          *mm = 1;
          *yy = *yy + 1;
      }
      else *mm = *mm + 1; //otherwise increase month by 1  
  }
      
    
    else { //if we can fit all the days in the same month
    *dd = *dd + days_left_to_add; //add the days
    days_left_to_add = 0; //reset days left
    }
  }
}




int main() {
// LEAP OR NOT LEAP
int year, mm, dd, yy, days_left_to_add;
//ask for input year
printf("Enter a year between 1800 and 10000: \n");
//check if its leap year and output answer
scanf("%i", &year);
  if (is_leap_year(year) == true) printf ("Leap\n");
  else printf ("Not Leap\n");

//ADD DAYS TO DATE
printf("Enter a date between years 1800 and 10000 with the format mm dd yy and provide the numbers of days you want to add to that 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);

}


Hello, @cloud5497628246 , and welcome to the forums.

What is the first part of your if statement checking?

How many days in December? :wink:

2 Likes

OMG thank you!!! Got it right now!!! Im so happy hahaha

2 Likes

Hello - really struggling with this task, and specifically my add_days_to_date function doesnt appear to be working properly.

When I run the code below, it prompts the first printf() statement and asks for the 4 variables. However, upon hitting enter it doesn’t print the output variables. However, when I hide (with //) the add_days_to_date function it correctly prints the entered numbers. Any guidance would be greatly appreciated.

#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;} 

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_in_month > 0) {
      days_left_in_month = days_in_month[*mm] - *dd;
      if (*mm == 2 && is_leap_year(*yy) == true) {
        days_left_in_month += 1;
        }
      if (days_left_to_add > days_left_in_month) {
        days_left_to_add = days_left_to_add - days_left_in_month - 1;
        *dd = 1;
        if (*mm < 12) {
          *mm++; 
          }
        else {
            *mm = 1;
           *yy++;
          }
        } 
        else {
          *dd = *dd + days_left_to_add;
          days_left_to_add = 0;
          }  
    }
}


int main() {
  int year, 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("%i %i %i %i", &mm, &dd, &yy, &days_left_to_add);

// this is where I believe the issue is
  add_days_to_date(&mm, &dd, &yy, days_left_to_add); 
  
  printf("%i/%i/%i\n", mm, dd, yy);