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

//
// how many more days can be added to the current month?
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
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
days_left_to_add = 0; //reset days left
}
}
}

int main() {
// LEAP OR NOT LEAP
int year, mm, dd, yy, days_left_to_add;
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");

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

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?

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

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;
}
*dd = 1;
if (*mm < 12) {
*mm++;
}
else {
*mm = 1;
*yy++;
}
}
else {
}
}
}

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

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