Thread: Calculate the number of Days between two dates

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Calculate the number of Days between two dates

    Ok, my book said I can use the following formula to calculate the days between two dates.

    N = 1461 x f(year, month) / 4 + 153 g(month /5 + day

    where

    f(year,month) = year-1 if month <=2
    year otherwise

    g(month) = month + 13 if month <=2
    month +1 otherwise

    But then it saids this formula only works for dates after March1, 1900( 1 must be added for dates form March 1, 1800 to February 28, 1900 and2 must be added for dates between March 1, 1700 to February 28, 1800

    Now I have to write a program that permits the user to typein two dates and then calculates the number of elapsed days.

    Ok this is what I have so far.
    Code:
    #include <stdio.h>
    Code:
    
    int main () {
     
    struct date {
     int month; 
    int day; 
    int year;
     };
     
    struct date d1, d2, N, N1, N2, x;
     int daynumber(struct date d);
     int elapsedDays;
     int days_between(struct date d1, struct date d2);
     
    printf ("Enter date 1 (MM/DD/YYYY): ");
     scanf ("%d/%d/%d", &d1.month, &d1.day, &d1.year);
     printF("\n");
     
    if ( d1.month <= 2 ) 
    //suppose to be something with f(year, month).
     // if( month <= 2){
     // f(year, month) = year - 1; g(month) = month + 13;
     { 
    int year = d1.year - 1;
     int month = d1.month + 13;
     }
     else
     {
     year = d1.year;
     month = d1.month + 1;
     }
     
    printf ("Enter date 2 (MM/DD/YYYY): ");
     scanf ("%d/%d/%d", &d2.month, &d2.day, &d2.year);
     printF("\n");
     
    if ( d2.month <= 2 ) 
    { 
    year = d2.year - 1;
     month = d2.month + 13;
     ?}
     else 
    { 
    year = d2.year;
     month = d2.month + 1;
     }
     if( d1 >= 03/01/1800 && d1 <= 02/28/1900)
     {
     N = N+1;
     }
     else if( d1 >= 03/01/1700 && d1 <= 02/28/1800)
     {
     N = N+2;
     }
     if( d2 >= 03/01/1800 && d2 <= 02/28/1900)
     {
     N = N+1;
     }
     else if( d2 >= 03/01/1700 && d2 <= 02/28/1800)
     {
     N = N+2;
     }
     // Computes N
     int daynumber(struct date d)
     {
     N1 = 1461 * f(d1.year,d1.month) / 4 + 153 * g(d1.month) / 5 + d1.day;
     N2 = 1461 * f(d2.year,d2.month) / 4 + 153 * g(d2.month) / 5 + d2.day;
     }
     
    // Computes elapsed days between two dates
     int days_between(struct date d1, struct date d2)
     {
     elapsedDays = N2 - N1;
     
    return elapsedDays;
     }
     
    // Function to determine if date d is before date x
     int isbefore(struct date d, struct date x); 
    int isbefore(struct date d, struct date x)
     {
     
    if ( d < x ) 
    x = 1; 
    else
     x = 0; 
    return x;
     }
     
    printf ("There are %d days between %d/%d/%d and %d/%d/%d", 
    elapsedDays, d1.month, d1.day, d1.year, d2.month, d2.day, d2.year);
     printF("\n");
     
    return 0; 
    }
    

    I can not get pass compiling the Program. I pretty new at this and can not figure out my mistakes
    Any help will be appreciated, Thanks, Bill

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Uh, did you do something funky with the font? It makes your code look unreadable.

    Anyway, your problem appears to be one of formatting. Indent your code properly and you should be able to find that you defined your struct in main unintentionally, and then you have mismatched braces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-23-2011, 10:24 PM
  2. 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
  3. number of days between 2 dates.
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2005, 07:30 AM
  4. days between dates formula
    By maes in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-12-2004, 12:52 PM
  5. C++ number of days between 2 dates?
    By Rain1522 in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2002, 09:09 PM