Like Tree2Likes
  • 2 Post By grumpy

Calculating the next day

This is a discussion on Calculating the next day within the C Programming forums, part of the General Programming Boards category; Hi there, Thanks again for those that helped with my Easter Sunday program. I was able to complete it. Currently ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    Calculating the next day

    Hi there,

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

    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?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,843
    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.
    stahta01 and xArt like this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    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);
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5,843
    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.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    So helpful! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with calculating a sum :/
    By blackolick_ in forum C Programming
    Replies: 6
    Last Post: 11-11-2012, 05:51 AM
  2. Calculating GCD
    By roelof in forum C Programming
    Replies: 15
    Last Post: 05-27-2011, 10:40 AM
  3. Calculating MPG
    By Robot Kris in forum C Programming
    Replies: 7
    Last Post: 09-08-2010, 08:29 PM
  4. calculating the mean
    By bartleby84 in forum C Programming
    Replies: 9
    Last Post: 08-27-2007, 11:47 AM
  5. calculating cos
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2005, 02:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21