Calculating the next day

• 03-14-2013
Anhur
Calculating the next day
Hi there,

Thanks again for those that helped with my Easter Sunday program. I was able to complete it. :rolleyes:

Currently I am working on a program where you enter in a date 14 03 2013 (Day, Month, Year) and you get the next day. I seem to be coming stuck with months with less than 31 days, and the whole leap year thing.

Here is my code so far.

Code:

```#include <stdio.h> int day, month, year, next_day, next_month, next_year, calculation; int main() {     printf("Enter a date in the form day/month/year: ");     scanf("%d %d %d", &day, &month, &year);     calculation = month - 1;     if (month == 2) {         if (year % 100 == 0) {             if (year % 400 == 0) {                 calculation = 29;             } else if (year % 4 == 0) {                 calculation = 29;             }             next_day = next_day +1;             next_month = month;             next_year = year;             if (next_day > calculation) {                 next_day = 1;                 next_month++;             }             if (next_month > 12) {                 next_month = 1;                 next_year++;             }             printf("The date of the next day is: %d %d %d", next_day, next_month, next_year);         }     } }```
Is there anything you can suggest to help me get my program working? :rolleyes:
• 03-14-2013
grumpy
At no point, after reading it, does your code make use of the variable day.

Your logic for computing number of days in a month is completely broken.

You need to look more closely at the logic for computing days in the month and detecting a leap year. According to your code, the number of days in a month is obtained by subtracting one from the month or (if month is 2) 29 in some circumstances.

Try writing a function called IsLeapYear() and another called DaysInMonth(). That will allow you to isolate relevant logic, rather than the screwy logic you are using.
• 03-14-2013
Anhur
Okay.

I've started writing my code again this time piece by piece. Focusing now on days and months (April, June, September and November).

Code:

```#include <stdio.h> int day, month, year; bool leapyear; int main() {     printf("Enter a date in the form day/month/year: ");     scanf("%d/%d/%d", &day, &month, &year);     day = day + 1;     if ((month = 4) || (month = 6) || (month = 9) || (month = 11) && (day = 30)) {         day = 1;         month + 1;     }     printf("The date of the next day is: %d/%d/%d", day, month, year); }```
• 03-15-2013
grumpy
How many equals signs are involved in a test for equality?

Also the operators || and && have different precedence, which you have not taken into account.
• 03-15-2013
Anhur