Quantcast
Channel: C - Codecademy Forums
Viewing all articles
Browse latest Browse all 25

Mini calendar solve

$
0
0

umm, this is my code, after remake cause the last one didnt work (i dont know why :)) ), hope it can help u <3

  • i write code in VS so it need #define _CRT_SECURE_NO_WARNINGS to use scanf.

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

//days in month
int days_in_month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

//check leap year
bool is_leap_year(int year) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
return true;
}
else {
return false;
}
}
//add some days to a date function
void add_days_to_date(int* mm, int* dd, int* yy, int days_left_to_add) {
int days_left_in_month = days_in_month[*mm] - *dd;

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

}

int main() {
int mm,dd,yy, days_left_to_add;
//check leap year
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”);
printf(“date:”);
scanf(“%i%i%i”, &mm, &dd, &yy);

if (is_leap_year(yy) == true) {
	printf("Leap year\n");

}
else {
	printf("Not leap year");
}
printf("days to add:");
scanf("%i", &days_left_to_add);
add_days_to_date(&mm, &dd, &yy, days_left_to_add);
printf("New date: %d/%d/%d", mm, dd, yy);

}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 25

Trending Articles