Finding the difference between two dates C++

Oct 9, 2016 at 7:08pm
Hi, I'm a student and I was wondering if anybody can help me debug one of my functions. For this program the user is suppose to input two dates in the format mm/dd/yy between the years of 1972-2071 and the program is suppose to output the difference between those two dates. All of my functions work except for the function that calculates the amount of days from 1/1/72. This is the way our professor would like us to do it with no extra features. Just a beginner version with a lot of if else statements and for loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  int ChangeToNumber(int m, int d, int y)
{
    int total=0;

    if(y<=71) //if the year is between 2000-2071
    {
        y+=28;

        for(int i=0; i<y; i++)
        {
            if(!LeapYear(y))
            {
                total+=365;
            }
            else
            {
                total+=366;
            }
        }
    }
    else //if the year is between 1972-1999
    {
        for(int i=72; i<y; i++)
        {
            if(!LeapYear(y))
            {
                total+=365;
            }
            else
            {
                total+=366;
            }
        }
    }


    for(int i=1; i<m; i++)
    {
        total+=DaysInMonth(m, y);
    }

    total += d;
    return total;
}
Oct 9, 2016 at 9:04pm
closed account (iN6fizwU)
Marie
I think you need to give us more information as to why exactly you think it isn't working. It is not possible to run it without functions
bool LeapYear( int y );
int DaysInMonth( int m, int y );
and either of these could actually be responsible.

If these functions are declared then the code does compile OK.

The only anomaly that immediately springs to mind is that you add 28 to y before you test it for being a leap year, and there are some peculiar rules around the turn of the century about leap years.
Oct 9, 2016 at 10:25pm
I've checked my other functions and I figured out it's only this function that is the problem. It has to do with my for loops I believe. This is what I have.


#include <iostream>

using namespace std;
void GetDate(string prompt, int m, int d, int y);
int CalculateDiff(int m1, int d1, int y1, int m2, int d2, int y2);
void Show(int number);
bool CorrectDate(int m, int d, int y);
int DaysInMonth(int m, int y);
bool LeapYear(int y);
int ChangeToNumber(int m, int d, int y);

int main(int argc, char *argv[])
{
int m1; //first date month
int d1; //first date date
int y1; //first date year
int m2; //second date month
int d2; //seond date day
int y2; //second date year
int difference; //difference between the two dates

GetDate("First Date: ", m1, d1, y1);
GetDate("Second Date: ", m2, d2, y2);
difference = CalculateDiff(m1, d1, y1, m2,d2, y2);
Show(difference);

return 0;
}

void GetDate(string prompt, int m, int d, int y)
{
char slash; //slash used when inputing the date


cout << prompt;
cin >> m >> slash >> d >> slash >> y;

while(!CorrectDate(m, d, y))
{
cout << "Invalid. ";
cin >> m >> slash >> d >> slash >> y;
}

}

int CalculateDiff(int m1, int d1, int y1, int m2, int d2, int y2)
{
int difference;
int dNum1; //total number of days from 1/1/72 to first date given
int dNum2; //total number of days from 1/1/72 to second date given

dNum1 = ChangeToNumber(m1, d1, y1);
dNum2 = ChangeToNumber(m2, d2, y2);

if(dNum1> dNum2) //if the first date total is larger subtract it first to avoid negatives
{
difference = dNum1 - dNum2;
return difference;
}
else
{
difference = dNum2 - dNum1;
return difference;
}

}

void Show(int number)
{
cout << "Difference: " << number;
cout << endl << endl << endl;

}

bool CorrectDate(int m, int d, int y)
{
if(y<0 || y>99)
{
return false;
}
if(m<1 || m>12)
{
return false;
}
if(d<1 || d>DaysInMonth(m, y))
{
return false;
}
return true;
}

int DaysInMonth(int m, int y)
{
int mday=0; //days in that month

switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
mday=31;
break;
case 4:
case 6:
case 9:
case 11:
mday=30;
break;
case 2:
if(LeapYear(y)) //if leap year feburary mdays=29
{
mday=29;
}
else
{
mday=28;
}
}
return mday;
}

bool LeapYear(int y)
{
if(y % 4)
{
return true;
}
return false;
}

int ChangeToNumber(int m, int d, int y)
{
int total=d;

if(y<=71) //if the year is between 2000-2071
{
y+=28;

for(int i=0; i<y; i++)
{
if(!LeapYear(y))
{
total+=366;
}
else
{
total+=365;
}
}
}
else //if the year is between 1972-1999
{
for(int i=72; i<y; i++)
{
if(!LeapYear(y))
{
total+=366;
}
else
{
total+=365;
}
}
}


for(int i=1; i<m; i++)
{
total+=DaysInMonth(m, y);
}

return total;
}


Oct 10, 2016 at 10:41am
I can't see anything wrong with your ChangeToNumber function. However, your method for getting the dates initially won't work (try printing out the values of m1, d1, y1 in main). This is because c++ passes function arguments by value, not reference.

Add the three & symbols to your function prototype and function declaration as below:
void GetDate(string prompt, int &m, int &d, int &y)

Then you will actually be able to get values for month, day, year from a void function.
Topic archived. No new replies allowed.