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);

The above if statement should have the condition (*mm = 12) as we are just talking about December here.

[quote=“packnsnacks, post:4, topic:647023”]

  else {
            *mm = 1;
           *yy++;
          }

[/quote] No need to increment the year here.

[quote=“packnsnacks, post:4, topic:647023”]

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

[/quote] try changing your is_leap_Year function body to => return (year % 4 == 0 && (year % 100 || year % 400 == 0)

hope this helps

Here the solution for the function

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; // Step 23 if (*mm == 2 && is_leap_year(*yy)) { // February leap year case => From 28 to 29 days. days_left_in_month++; } if (days_left_to_add >= days_left_in_month) { // if more days left can be add in a month days_left_to_add -= days_left_in_month; // 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 ? days_left_to_add--; if (*mm != 12) { // if last month is Not December *mm = *mm + 1; // pass to the next month } else { *mm = 1; // if it is December *yy = *yy + 1; // pass to the next month => January } } else { // (days_left_to_add < days_left_in_month) *dd = *dd + days_left_to_add; // simply add the days days_left_to_add = 0; // no more days to add ! } } }

Hello everyone!

I am having a problem with my mini calendar. I wrote it in C. Everything works just fine but the only problem is that when I enter the date the program return “Segmentation fault”. I am pretty new to all of this and do not really understand what this problem really is and how to resolve it. Below is my code. Any help would be greatly appreciated!

Thanks and have a nice day!

#include <stdio.h> #include <stdbool.h> bool is_leap_year (int year) { if (year % 4 == 0) { if (year % 100 == 0) {if (year % 400 == 0) {return true;} else return false;} else return true;} else return false; } 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* yyyy, 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(*yyyy) == true) { days_left_in_month++; } 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 = 1; *yyyy++; } else {*mm++;} } else { *dd = *dd + days_left_to_add; days_left_to_add = 0; } } } int main() { /* int enter_number; printf("Leap year check!"); scanf("%d", &enter_number); if ( is_leap_year (enter_number) == true) {printf("True");} else {printf("False");} */ int mm, dd, yyyy, days_left_to_add; printf("Enter a date between year 1800 and 10000 in the format mm dd yy and provide a number to add to this date:"); scanf("%d %d %d %d", &mm, &dd, &yyyy, &days_left_to_add); add_days_to_date(&mm, &dd, &yyyy, days_left_to_add); printf("%d %d %d", mm, dd, yyyy); }