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;
}
[duplcate, duplicate post. See http://www.cplusplus.com/forum/beginner/275820/ ]
Last edited on
And the original post seems to have been deleted.
[Edit. The link was bad and has been fixed. See below]

In the code above, line 67 should move outside the loop.
Last edited on
And the original post seems to have been deleted.

The link appears to be mucked up by appending a closing bracket, confusing the forum's URL parser.
Ah. OK. Fixed by adding spaces before the ]
Topic archived. No new replies allowed.