Originally Posted by
tammysmith
I'm trying to write a c program that determines the day number (1 to 366) in a year for a date provided by a user.
How do I adjust this program for a leap year like (december 31,2012) is day 366 since it was a leap year. Is there a better way(I really want to keep the functions and the switch ? I am also getting an error (expected primary expression before token on line 82)This is my code so far please help
You should check the input from user.
Code:
…
int days_per_month[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
…
// after the user entered the input in main
// check year
if (year < 1601) {
fprintf(stderr, "The gregorian calendar does not exist in year %d! exit.\n", year);
return 1;
}
leapyear = isleap(year);
// check month
if (month < 1 || month > 12) {
fprintf(stderr, "Invalid month %d! exit.\n", month);
return 2;
}
// check day
if (day < 1 || day > days_per_month[month -1]) {
fprintf(stderr, "Invalid day %d! exit.\n", day);
return 3;
}
if (month == 2 && leapyear == 0 && day > 28) {
fprintf(stderr, "Invalid day %d! exit.\n", day);
return 3;
}
// all inputs are valid
…
Your function isleap looks fine, but i have not tested.
You don't use all parameters in your function displaydate and give 4 integers to printf, but in the format, there are only 2 formatspecifier for integers.
Please correct this.
Your error is the empty default-case. With the checks in front, you don't need a default-case. Delete it.