function days from date to december 31st

This is a discussion on function days from date to december 31st within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <time.h> int days, month, year, days_1, month_1, year_1, leapy, ndays, ddays, days_total, days_beforeleap; int daysinmonth(int month) ...

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

    function days from date to december 31st

    Code:
    #include <stdio.h>
    #include <time.h>
    int days, month, year, days_1, month_1, year_1, leapy, ndays, ddays, days_total, days_beforeleap;
    
    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;
            }
    
    }
    int daystoyear(month, month_1, year, year_1)
    {
    int i=0, days_toend, years_todays;
    days_toend=daysinmonth(month)-days;
            while(month<month_1)
            {
            daysinmonth(month);
            month++;
            }
    years_todays=365*(year_1-year);
    days_beforeleap=days_toend+years_todays;
    return days_beforeleap;
    }
    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);
    daystoyear(month, month_1, year, year_1);
    printf("total days=%d",days_beforeleap);
    }
    the function days in year is supposed to read the number of days from a date to the end of the year. I don't think the while loop is working but I don't know why. Is it a recursive function problem?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    for(i = month+1;i < month_1 - 1;++i); /* <<-- here is your newbie mistake */
    days_total += daysinmonth(i);
    days_total += days_1;
    return days_total;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    I think that for loop is what causes my date totals to be 60 and 61 for 1/1/1 to 1/1/1 and 1/1/1 to 1/1/2. I've toyed with it a bit, but I can't seem to find the exact logic problem.

    Am I overthinking it? would this work?
    Code:
    for(i=month;i<month_1;++i);

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Delete line 60 and see what happens...

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    AHA! the semicolon! Thanks, and hello again tater, always a pleasure!

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    ok, it now works for dates in the same month(and year of course), but the values come up about two months short for long amounts of time. For example, 1/1/1 to 12/31/1 comes out to 305 days. Here's the new function:
    Code:
    daysinyear(int days, int month, int days_1, int month_1)
    {
    int i;
    for(i = month+1;i < month_1 - 1;++i)
    days_total += daysinmonth(i);
    days_total+=days_1;
    days_total=days_total-1;
    return days_total;
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    Oh, and I added line 7 because the values in the same month were 1 day off, but I'm not sure how wise that was now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-23-2011, 09:24 PM
  2. Adding #days to a date
    By acidblue in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2011, 01:36 PM
  3. Replies: 47
    Last Post: 01-04-2008, 04:24 AM
  4. current date - 3 days ago
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 03:21 PM
  5. How to add Days to a Date?
    By Bufferlo in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 09:19 AM

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