C++ class

Program only lets user to insert start date, output is completely wrong...

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  #include <iostream>
 #include <cmath>

 using namespace std;

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

    public:
    Date(int, int, int);
    int getLeapYears();
    int getTotalDays();
    int operator-(Date&);
  };

   int main()
   {
    int day, month, year;
    char c;

    cout << "Enter a start date (m/d/y): " << endl;
    cin >> month >> c >> day >> c >> year;

    Date start = Date(month, day, year);

    cout << "Enter an end date (m/d/y): " << 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(int a, int b, int c)
    {
    m = a;
    d = b;
    y = c;
    }

    const int monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

int Date::getLeapYears()
{
     int years = y;
     if (m <= 2)
         years--;
     return years / 4 - years / 100 + years / 400;
}


int Date::getTotalDays()
{
     int n1 = y*365 + d;
     for (int i=0; i<m - 1; i++)
     {
         n1 += monthDays[i];
         n1 += getLeapYears();
     }
     return n1;
}

int Date::operator-(Date& d) {
    int difference = getTotalDays() - d.getTotalDays();
    return difference;
}
Last edited on
I havn't thoroughly tested this but from the troubleshooting i've done and limited testing it seems to work correctly. keep in mind that 1/1-1/3 only has one day in between because you aren't counting the start or the end.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
//#include <cmath>

using namespace std;

class date
{
private:
    int m;
    int d;
    int y;
    const int monthDays[12] = { 31,28,31,30,31,30,31,31,31,31,30,31 };
public:
    date(int a, int b, int c)
    {   
        m = a;
        d = b;
        y = c;
    }
    int CountToEndDate(date& other) {

        if (this->m == other.m) {
            return this->d - other.d - 1;
        }

        int c = this->d + monthDays[other.m-1] - other.d - 1;

        for(int i = other.m; i < this->m-1;i++){
            c += monthDays[i];
        }

        return c;
    }

    int CountToBegin() {
        int c = 0;
        for (int i = 0; i < this->m-1; i++) {
            if (this->m - 1 != i) {
            c += monthDays[i];
            }
            
        }
        c += this->d-1;
        //cout << c << "\n";
        return c;
    }
    int CountToEnd() {
        int c = 0;
        for (int i = this->m; i < 12; i++) {
            c += monthDays[i];
        }
        c += (monthDays[this->m-1] - this->d-1) > 0 ? monthDays[this->m - 1] - this->d - 1 : 0;
        //cout << c << "\n";
        return c;
    }
    int YearsBetween(date& other) {
        if(this->y > other.y){ return this->y - other.y - 1;}
        else if (other.y > this->y) { return other.y - this->y - 1; }
        else { return 0; }
    }
    int countLeapYears(date& d)
    {
        if (this->y == d.y) { return 0; } //this is line probably incorrect.

        int c{ 0 };
       // cout << this->y << "\n";
        for (int i = d.y; i < this->y + 1; i++) {
            if (isLeap(i)) {
                c++;
            }
        }

        if (isLeap(d.y)) {
            if (d.m > 2) {
                c--;
            }
        }

        if (isLeap(this->y)) {
            if (this->m == 2 && this->d < 29) { c--; }
            else if (this->m < 2) { c--; }
        }
        return c;
    }

    bool isLeap(int year) {
        if (year % 4 == 0 && year % 100 != 0) {
            return true;
        }
        else if (year % 4 == 0 && year % 100 == 0 && year % 400 == 0) {
            return true;
        }
        return false;
    }
    int getDifference(date& other)
    {
        if (this->y == other.y) {
            return this->CountToEndDate(other)+this->countLeapYears(other);
        }
        else {

           return this->CountToBegin() + other.CountToEnd() + (this->YearsBetween(other) * 365) + this->countLeapYears(other);
        }
    }
    int operator-(date& d) {
        return this->getDifference(d);
    }
};

int main()
{
    int day{}, month{}, year{};
    //char c;

  /*  cout << "Enter a start date: " << endl;
    cout << "month" << "\n";
    cin >> month; 
    cout << "day" << "\n";
    cin >> day;
    cout << "year" << "\n";
    cin >> year;*/

    date start = date(1, 1, 2020);

  /*  cout << "Enter an end date: " << endl;
    cout << "month" << "\n";
    cin >> month;
    cout << "day" << "\n";
    cin >> day;
    cout << "year" << "\n";
    cin >> year;*/

    date end = date(1,3, 2021);
    int duration = end - start;

    cout << "The number of days between those two dates are: " <<
        duration << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.