I am almost done with my code but whenever I try to test it, it displays this error:
In function ‘add_days_to_date’:
error: ‘else’ without a previous ‘if’. However, when I look at other codes here in the forum, they all look the same as the way I wrote it. Can someone help me figure out what is wrong?
#include <stdio.h>
#include <stdbool.h>
bool is_leap_year(int year){
//year not divisible by 4 is NOT leap
if(year % 4 != 0){
return false;
}
//year divisible by 4 and not divisible by 100 IS leap
else if(year % 100 != 0){
return true;
}
//year divisible by 400 IS leap
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;
//how many days can be added before month ends
while (days_left_to_add > 0){
int days_left_in_month = days_in_month[*mm] - *dd;
//if is leap year, increase february to 29 days
if (days_in_month[2] && is_leap_year(*yy) == true){
days_left_in_month ++;
}
if(days_left_to_add > days_left_in_month){
days_left_to_add -= days_left_in_month + 1;
*dd = 1;
}
if(*mm == 12){
*mm = 1;
*yy = *yy + 1;
}
else{
*mm = *mm + 1;
}
else{
*dd = *dd + days_left_to_add;
days_left_to_add = 0;
}
}
}
int main() {
/*
int year;
//ask to input year
printf("input a number between 1800 and 10000: \n");
//scan for year inputed
scanf("%i", &year);
//check to see if input is a leap year
if (is_leap_year(year) == true) printf("Leap Year\n");
else printf("Not Leap Year\n");
*/
int mm;
int dd;
int yy;
int 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);
add_days_to_date(&mm, &dd, &yy, days_left_to_add);
printf("%i%i%i\n", mm, dd, yy);
}