Thread: Difference between two dates C++

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    5

    Difference between two dates C++

    I am trying to solve a problem that asks me to give the total days between two dates. I have to take care of the some matters between those two dates such as leap years and the way of inputting the years by the users. (For example, if you input 1 and 17, the code will still give you the difference is 16 years (2017 - 2001 = 16). I am not supposed to change ANYTHING inside the main() function. Here is my code.
    inser
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    class date
    {
        private:
        int m;
        int d;
        int y;
        
        public:
        date();
        date(int, int, int);
        int countLeapYears(date&);
        int getDifference1(date&);
        int getDifference2(date&);
        
        
    };
    
    int main()
    {
        int day, month, year;
        char c;
        
        cout << "Enter a start date: " << endl;
        cin >> month >> c >> day >> c >> year;
        
        date start = date(month, day, year);
        
        cout << "Enter an end date: " << endl;
        cin >> month >> c >> day >> c >> year;
        
        date end = date(month, day, year);
        
        int duration = end - start;
        
        cout << "The number of days between those two dates are: " << duration << endl;
        
        return 0;
    }
    
    date::date()
    {
        m = 0;
        d = 0;
        y = 0;
    }
    
    date::date(int a, int b, int c)
    {
        m = a;
        d = b;
        y = c;
    }
    
    const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    int date::countLeapYears(date& d)
    {
        int years = d.y; 
        if (d.m <= 2)
            years--;   
        return years / 4 - years / 100 + years / 400;
    }
    
    int date::getDifference1(date& start)
    {
    
        int n1 = start.y*365 + start.d;
       
        for (int i=0; i<start.m - 1; i++)
        {
          n1 += monthDays[i];
          n1 += countLeapYears(start);
        }
         
        return n1;
    }
    
    int date::getDifference2(date& end)
    {
        int n2 = end.y*365 + end.d;
       
        for (int i=0; i<end.m - 1; i++)
        {
          n2 += monthDays[i];
          n2 += countLeapYears(end);
        } 
        return n2;
    }
    I have an issue with my code above, and I need your help please. When I ran it, it said invalid binary operation between "date" and "date". Now, I assume that when I initialized int duration = end - start, I should have got a number. I guess what I am doing wrong here is I failed to convert the (end - start) date type into integer. I thought my function getDifference already took care of that issue. Somehow, it appeared that I did not take care of that issue.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > int duration = end - start;
    Unless you want to get into operator overloading, you need to do something like

    int duration = end.getDifference1(start);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating the difference between two dates
    By Fiskker in forum C Programming
    Replies: 10
    Last Post: 05-28-2015, 09:32 PM
  2. GUI for difference between two dates
    By amroto in forum C++ Programming
    Replies: 3
    Last Post: 07-10-2012, 04:52 AM
  3. Difference between dates
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-18-2008, 08:18 AM
  4. Getting the difference between dates
    By Leftos in forum C Programming
    Replies: 7
    Last Post: 01-20-2008, 12:49 AM
  5. difference between dates
    By rahulsk1947 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2007, 09:57 AM

Tags for this Thread