Difference between two dates C++

In summary: And yes, countLeapYears is a poorly chosen name in this context.)In summary, the conversation is about a problem involving calculating the total days between two dates while taking into consideration leap years and proper input from users. The code provided has an issue with the getDifference function not properly considering the end date. The code is then updated to fix this issue, but still has a mistake in the countLeapYears function.
  • #1
Hughng
26
0

Homework Statement


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.

Homework Equations


#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 getDifference(date&, 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::getDifference(date& start, date& end)
{

int n1 = start.y*365 + start.d;

for (int i=0; i<start.m - 1; i++)
{
n1 += monthDays;
n1 += countLeapYears(start);
}

int n2 = end.y*365 + end.d;

for (int i=0; i<end.m - 1; i++)
{
n2 += monthDays;
n2 += countLeapYears(end);
}
int duration = n2 - n1;
return duration;
}

The Attempt at a Solution


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.
 
Physics news on Phys.org
  • #2
Your code has no idea that the minus sign should trigger the getDifference function. With the current functionality you would have to use int duration = date::getDifference(end, start);. To use the minus sign you have to overload the minus operator.
 
  • #3
let me update my code.
#include <iostream>
#include <cmath>

using namespace std;

class date
{
private:
int m;
int d;
int y;

public:
date(int, int, int);
int countLeapYears(date&);
int getDifference(date&);
friend int operator-(const date&);

};
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::getDifference(date& start)
{

int n1 = start.y*365 + start.d;

for (int i=0; i<start.m - 1; i++)
{
n1 += monthDays;
n1 += countLeapYears(start);
}

return n1;
}

int date operator-(const date& d)
{
int difference = getDiffernce(d);
return difference;
}

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;
}
 
  • #4
You can write [i] as [plain][i][/plain] to avoid that the forum interprets it as marker to make the text italic. Or put the whole code in [code]...[/code]-tags, then things like indentation stay visible.

The new code is better, but exactly like that it won't even compile.
 
  • #5
it gave me the wrong total difference days between two input dates.
It gave me

727081
as the difference between 1/1/1992 and 1/1/2016
 
  • #6
Where does your getDifference consider the other date?

727081/365.25 = 1990.6
 
  • #7
What do you mean by that? Please help me
 
  • #8
Code:
int date::getDifference(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;
}
Does the output change in any way if you have a different end date? Can you calculate the difference between two dates if you don't even consider the end date?

An unrelated error: you add days for LeapYears every month. 0 times in your example, but up to 11 times if the start date is in December. You should add them exactly once.
 

1. How do I calculate the difference between two dates in C++?

To calculate the difference between two dates in C++, you can use the time_t data type and the difftime() function. This function takes in two time_t values representing the two dates and returns the difference between them in seconds.

2. Can I use the date library in C++ to calculate the difference between two dates?

Yes, you can use the date library in C++ to calculate the difference between two dates. This library provides the date::duration class which allows you to represent a duration of time between two dates.

3. How do I handle leap years when calculating the difference between two dates in C++?

In C++, you can use the date::year_month_day class from the date library to handle leap years when calculating the difference between two dates. This class takes into account leap years and provides functions to check for leap years.

4. Is there a way to format the output of the calculated difference between two dates in C++?

Yes, you can use the date::format() function from the date library to format the output of the calculated difference between two dates in C++. This function allows you to specify a format string to display the duration in a specific format.

5. Can I calculate the difference between two dates in C++ using time zones?

Yes, you can use the date::zoned_time class from the date library to calculate the difference between two dates in C++ using time zones. This class takes into account time zone differences when calculating the duration between two dates.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
759
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
929
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
893
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top