Hi I just starting learning C and have an assigment to hand in tomorrow which is supposed to tell you the next date after the day entered i.e todays date is 19/3/2014 tomorrows date should be 20/3/2014
I have sorted it so it calculates the leap year exceptions for february and if the last day is the 31st it should start a new month and that the last day of the year the next day will be the first day of the new year.
The problem I'm having is where there are only 30 days in the month - i must be doing something wrong with the else if's but can't figure out what - here is the code so far
Code:
#include <stdio.h>
#include <stdbool.h>
int day, month, year, nextday, nextmonth;
bool leapyear;
int main() {
printf ("Enter a date in the form day/month/year: ");
scanf ("%d/%d/%d", &day, &month, &year);
nextday = day + 1;
leapyear = false;
if (year % 4 == 0) {
leapyear = true;
}
if (year % 100 == 0) {
leapyear = false;
}
if (year % 400 == 0) {
leapyear = true;
}
if (leapyear) {
if ((day == 28) && (month == 2)) {
if (leapyear) {
nextday = day + 1;
}
}
} else if ((day == 28) && (month == 2)) {
nextday = 1;
month = month + 1;
} else if ((day == 31) && (month == 12)) {
nextday = 1;
month = 1;
year = year + 1;
} else if ((day < 31) && (month == 1 || 3 || 5 || 7 || 8 || 10 )) {
nextday = day + 1;
month = month;
} else if ((day == 31) && (month == 1 || 3 || 5 || 7 || 8 || 10 )) {
nextday = 1;
month = month + 1;
} else if ((day < 30) && (month == 4 || 6 || 9 || 11)) {
nextday = day + 1;
month = month;
} else if ((day == 30) && (month == 4|| 6 || 9 || 11)) {
nextday = 1;
month = month + 1;
} else if ((day < 31) && (month == 12)) {
nextday = day + 1;
month = month;
}
printf ("%d/%d/%d", nextday, month, year);
}