Thread: writing A function which gives the number of days between two dates in the same year

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    writing A function which gives the number of days between two dates in the same year

    alright, I've been working on a program that will give the number of days between two calendar dates. It is suggested that we write a function that gives the number of days between two dates in a year. I can see how this would work, because then I could multiply that number of days by 365+ leap years, but I can't figure out the logic behind that function. Here is my program so far to show what I have.
    Code:
    #include <stdio.h>
    #include <time.h>
    int days, month, year, days_1, month_1, year_1, leapy, ndays;
    
    int daysinmonth(int month) /*there is a problem in this statement, your printf is reading out ridiculous numbers for days, don't forget to ask*/
    {
            switch(month)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                    return 31;
            case 2:
                    return 28;
            case 4:
            case 6:
            case 9:
            case 11:
                    return 30;
            default:
                    return -1;
            }
    }
    int leap(int year)
    {
            if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
            {
            leapy=1;
            }
            else
            {
            leapy=0;
            }
    
    }
    
    double difftime(time, time_1)
    {
    printf("difftime(month, month_1)=%d",difftime(month, month_1));
    }
     
    
    int main(void)
    {
    int days_1, month_1, year_1;
    char a, b, a1, b1;
    printf("intput a date in the follwing format:\n");
    printf("mmddyyyy: in other words the two digit day,two digit month,\n");
    printf("and four digit year, with no slashes or heifens anywhere.\n");
    scanf("%d%c%d%c%d", &month, &a, &days, &b, &year);
            if(month<0 || month>12 || days<0 || days>31 || year<0)
            {
            printf("data not valid");
            }
    printf("enter the later date in the same format:\n");
    scanf("%d%c%d%c%d", &month_1, &a1, &days_1, &b1, &year_1);
    ndays=daysinmonth(month);
    printf("days in month=%d\n", ndays);
    leap(year);
    printf("%d", leap);
     
    difftime(month, month_1);
    return 0;
    }
    My switch function works well, so I get the correct number of days in a month (thanks to tater) returned, and I've messed around with using the difftime function and dividing by 86,400 to get the proper number of days, but that hasn't worked. So my question is, how do I exactly go about writing this function that will give me the number of days between two dates in the same year?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How about something like:
    Code:
    int days_total = daysinmonth(month) - days;  // Get the first month's days
    for(i = month + 1;i < month_1 - 1;++i)  // Add all of the intervening month's days
      days_total += daysinmonth(i);
    days_total += days_1;                              // Add the days from the final month
    You're going to have to figure out how to call leap() from daysinmonth(), otherwise February will not be reliable.
    Last edited by itsme86; 11-23-2011 at 07:39 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    what if I changed my switch statement to this...
    Code:
     switch(month)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                    return 31;
            case 2:
                    (if(leap(year)=0)
                    return 28
                    else return 29;
            case 4:
            case 6:
            case 9:
            case 11:
                    return 30;
            default:
                    return -1;
            }

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    where is your break code after case? i think you should use break code after every case, otherwise, it will always return -1 or 30.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rac1 View Post
    where is your break code after case? i think you should use break code after every case, otherwise, it will always return -1 or 30.
    Code:
     switch(month)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                    return 31;
            case 2:
                    return 28
            case 4:
            case 6:
            case 9:
            case 11:
                    return 30;
            default:
                    return -1;
            }
    You don't want break statments in there... you want it to fall through till it finds a return statement at which case the function returns with the stated value. In effect it works like a great big OR statement...

    The real problem with the version in message #1 is in line 41... He's using a library function name as a local function name then calling the function from within... in fact, what he's doing is recursively calling his own function with no escape clause.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    Code:
    #include <stdio.h>
    #include <time.h>
    int days, month, year, days_1, month_1, year_1, leapy, ndays, ddays, days_total;
    
    int daysinmonth(int month) /*there is a problem in this statement, your printf is reading out ridiculous numbers for days, don't forget to ask*/
    {
            switch(month)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                    return 31;
            case 2:
                    return 28;
            case 4:
            case 6:
            case 9:
            case 11:
                    return 30;
            default:
                    return -1;
            }
    }
    int leap(int year)
    {
            if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
            {
            leapy=1;
            }
            else
            {
            leapy=0;
            }
    
    }
    daysinyear(int days, int month, int days_1, int month_1)
    {
    int i=0;
    days_total = daysinmonth(month) - days;
    for(i = month+1;i < month_1 - 1;++i);
    days_total += daysinmonth(i);
    days_total += days_1;
    return days_total;
    }
    
    int main(void)
    {
    int days_1, month_1, year_1;
    char a, b, a1, b1;
    printf("intput a date in the follwing format:\n");
    printf("mmddyyyy: in other words the two digit day,two digit month,\n");
    printf("and four digit year, with no slashes or heifens anywhere.\n");
    
    scanf("%d%c%d%c%d", &month, &a, &days, &b, &year);
            if(month<0 || month>12 || days<0 || days>31 || year<0)
            {
            printf("data not valid");
            }
    printf("enter the later date in the same format:\n");
    scanf("%d%c%d%c%d", &month_1, &a1, &days_1, &b1, &year_1);
    daysinmonth(month);
    daysinmonth(month_1);
    printf("days_total=%d",daysinyear(days, month, days_1, month_1));
    return 0;
    }
    When I run this code it reads out 60 days for the dates 1/1/1 to 1/2/1. The logic you use seems good, I'm wondering if it's maybe the way I'm calling the daysinmonth or daysinyear function? thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can i get the number of days between two dates?
    By acohrockz21 in forum C Programming
    Replies: 3
    Last Post: 02-29-2008, 10:43 AM
  2. number of days between 2 dates.
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2005, 07:30 AM
  3. Counting Number of days from year zero
    By wireless in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2002, 07:31 AM
  4. C++ number of days between 2 dates?
    By Rain1522 in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2002, 09:09 PM
  5. Calculating days between two Dates
    By Niy in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2002, 08:16 PM