Hello! I am on the “Mini Calendar” challenge in the C skill path. It seems all fine, I get the code running, there are no compile errors. But when I enter the date and days to add, nothing appears. Can you see what is wrong? maybe something with the scanf() or print() at the end (in the the main function.) ? Link to task “Mini Calendar”
Thank you for giving me a clue!
#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;
}
/* --- ALTERNATIVE WAY WITH AND OR OPERATORS ------
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
return true;
} else {
return false;
}*/
}
int days_in_month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
/* ADD DAYS TO DATE */
void add_days_to_date (int* dd, int* mm, 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++;
}
}
if (days_left_to_add > days_left_in_month) { // if more days left than can be added in a month
days_left_to_add -= days_left_in_month + 1;
// 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?
if (*mm == 12) { // if last month was December
*mm = 1; // next month is January
*yy += 1; // and we are in a new year so increase by 1
} else *mm += 1; // if some other month, just increase by 1
} else { // if all days can be added in this month
*dd = *dd + days_left_to_add; // simply add the days
days_left_to_add = 0; // no more days to add!
}
}
/* MAIN FUNCTION*/
int main() {
int dd;
int mm;
int yy;
int days_left_to_add;
printf("Please enter a date between the years 1800 and 10000 and provide the number of days to add to this date. Use the following format for input dd mm yy nn (separated by space)\n");
scanf("%d%d%d%d", &dd, &mm, &yy, &days_left_to_add);
add_days_to_date(&dd, &mm, &yy, days_left_to_add);
printf("%d %d %d\n", dd, mm, yy);
/*int year;
printf("Enter a year between 1800 and 10000: ");
scanf("%d", &year);
is_leap_year(year);
if (is_leap_year(year) == true) {
printf("This is a Leap Year\n");
} else {
printf("This is Not a Leap Year\n");
}*/
}
4 posts - 3 participants