Program that calculates age in days - are my calculations correct?

I've written a program which allows the user to enter in a year, month and a date, and the program calculates the duration between that date and September 20, 2013 in days.

When I type in 1 1 1 (i.e. January 1, year 1), I get 735145 days. However, I've tried typing in the same date on online applications, and they don't agree with this result. This is my code:
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
//
//  main.cpp
//  Ålder i dagar
//
//  Created by Måns Nilsson on 2013-09-20.
//  Copyright (c) 2013 Måns Nilsson. All rights reserved.
//

#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <cmath>
#include <ctime>
#include <math.h>
#include <unistd.h>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, const char * argv[])
{
    int y_1, m_1, d_1, y_2 = 2013, m_2 = 9, d_2 = 20, d = 0, manad[12];
    
    cout<<"Ange ditt födelseår i siffror: ";
    cin >>y_1;
    
    cin.clear();
    
    cout<<"Ange din födelsemånad i siffror: ";
    cin >>m_1;
    
    cin.clear();
    
    cout<<"Ange din födelsedag i siffror: ";
    cin >>d_1;
    
    manad[0] = 31;
    manad[1] = 0;
    manad[2] = 31;
    manad[3] = 30;
    manad[4] = 31;
    manad[5] = 30;
    manad[6] = 31;
    manad[7] = 31;
    manad[8] = 30;
    manad[9] = 31;
    manad[10] = 30;
    manad[11] = 31;
    
    if (y_1%4 == 0) {
        manad[1] = 29;
    }
    else {
        manad[1] = 28;
    }
    
    if (y_2 > y_1) {
        d += manad[m_1 - 1] - d_1;
    }
    else if (y_2 == y_1 && m_2 > m_1) {
        d += manad[m_1 - 1] - d_1;
    }
    else if (y_2 == y_1 && m_2 == m_1){
        d = d_2 - d_1;
    }
    else { }
    
    if (y_2 == y_1 && m_2 > m_1 + 1) {
        for (int k = m_1 + 1;k<m_2;k++) {
            d += manad[k - 1];
        }
    }
    else {
        
    for (int k = m_1 + 1;k<13 && y_1<y_2;k++) {
        d += manad[k - 1];
    }
    }
    
    for (int k = y_1 + 1;k<y_2;k++) {
        if (k%4 == 0) {
            d += 366;
        }
        else {
            d += 365;
        }
        
    }
    
    if (y_1 < y_2) {
    for (int k = 1;k<m_2;k++) {
        if (y_2%4 == 0 && k == 2) {
            d += 29;
        }
        else if (k == 2) {
            d += 28;
        }
        else {
            d += manad[k - 1];
        }
    }
        d += d_2;
          }
    else if (y_1 == y_2 && m_1 < m_2) {
        d += d_2;
    }
    else { }
    
    cout<<d;
}


Does the program correctly calculate the duration to 735145 days? It does take leap years into account.
> When I type in 1 1 1 (i.e. January 1, year 1), I get 735145 days.
> However, I've tried typing in the same date on online applications,
> and they don't agree with this result

Test your code with input dates after 1582. The Gregorian calendar did not exist before October 15, 1582.

The Gregorian reform modified the Julian calendar's scheme of leap years as follows:

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year. - http://en.wikipedia.org/wiki/Gregorian_calendar
I've updated my code accordingly. I also made sure that you can't enter a date before October 15, 1582, and I removed unnecessary library includes at the beginning of the code.

If I enter 1582 10 25 (i.e. October 25, 1582), I get 157385 days. The program performs the following calculations:
* There are 67 days from October 25, 1582 until December 31, 1582 (6 for October, 30 for November, 31 for December).
* From 1583 through 2012, there are 430 years, of at least 365 days in duration each (365 * 430 = 156950 days).
* From October 25, 1982 through 2012, there are, in principal, 108 leap days.
* The three leap years in 1700, 1800 and 1900 aren't leap years, so they're subtracted from 108, giving 105 leap days. 2000 still is, and counts as, a leap year because it's exactly divisible by 400.
* From December 31, 2012 until September 20, 2013, there are 263 days (no leap year).
* Added together: 67 + 156950 + 105 + 263 = 157385 days from October 25, 1582 until September 20, 2013.

I'm sorry for being stupid enough not to realize that calendars haven't existed forever. Your text reference, JLBorges, is very helpful. I'm quite surprised that my program has not failed a single time to calculate the duration correctly, no matter what starting date I enter. I've only been programming for a month.

Here is my code ("manad" means "month" in English):
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{
    int y_1, m_1, d_1, y_2 = 2013, m_2 = 9, d_2 = 20, d = 0, manad[12];
    
    cout<<"Enter your birth year in numbers: ";
    cin >>y_1;
    for (int k = 0;y_1<1582;k++) {
        if (k == 3) {
            cout<<"Since you still haven't understood this information, the program will now exit.";
            return 0;
        }
        else {
            cout<<"The Gregorian calendar did not exist before October 15, 1582; please choose another year: ";
            
            cin.clear();
            
            cin >>y_1;
        }
    }
    
    cin.clear();
    
    cout<<"Enter your birth month in numbers: ";
    cin >>m_1;
    if (y_1 == 1582) {
    for (int k = 0;m_1<10;k++) {
        if (k == 3) {
            cout<<"Since you still haven't understood this information, the program will now exit.";
            return 0;
        }
        else {
            cout<<"The Gregorian calendar did not exist before October 15, 1582; please choose another month: ";
            
            cin.clear();
            
            cin >>m_1;
        }
    }
           }
    else { }
    
    cin.clear();
    
    cout<<"Enter your birth day in numbers: ";
    cin >>d_1;
    if (y_1 == 1582 && m_1 == 10) {
        for (int k = 0;d_1<15;k++) {
            if (k == 3) {
                cout<<"Since you still haven't understood this information, the program will now exit.";
                return 0;
            }
            else {
                cout<<"The Gregorian calendar did not exist before October 15, 1582; please choose another day: ";
                
                cin.clear();
                
                cin >>d_1;
            }
        }
    }
    else { }
    
    manad[0] = 31;
    manad[1] = 0;
    manad[2] = 31;
    manad[3] = 30;
    manad[4] = 31;
    manad[5] = 30;
    manad[6] = 31;
    manad[7] = 31;
    manad[8] = 30;
    manad[9] = 31;
    manad[10] = 30;
    manad[11] = 31;
    
    if (y_1%400 == 0) {
        manad[1] = 29;
    }
    else if (y_1%100 == 0) {
        manad[1] = 28;
    }
    else if (y_1%4 == 0) {
        manad[1] = 29;
    }
    else {
        manad[1] = 28;
    }
    
    if (y_2 > y_1) {
        d += manad[m_1 - 1] - d_1;
    }
    else if (y_2 == y_1 && m_2 > m_1) {
        d += manad[m_1 - 1] - d_1;
    }
    else if (y_2 == y_1 && m_2 == m_1){
        d = d_2 - d_1;
    }
    else { }
    
    if (y_2 == y_1 && m_2 > m_1 + 1) {
        for (int k = m_1 + 1;k<m_2;k++) {
            d += manad[k - 1];
        }
    }
    else {
        
    for (int k = m_1 + 1;k<13 && y_1<y_2;k++) {
        d += manad[k - 1];
    }
    }
    
    for (int k = y_1 + 1;k<y_2;k++) {
        if (k%400 == 0) {
            d += 366;
        }
        else if (k%100 == 0) {
            d += 365;
        }
        else if (k%4 == 0) {
            d += 366;
        }
        else {
            d += 365;
        }
        
    }
    
    if (y_1 < y_2) {
    for (int k = 1;k<m_2;k++) {
        if (k == 2) {
            if (y_2%400 == 0) {
                d += 29;
            }
            else if (y_2%100 == 0) {
                d += 28;
            }
            else if (y_2%4 == 0) {
                d += 29;
            }
            else {
                d += 28;
            }
        }
        else {
            d += manad[k - 1];
        }
    }
        d += d_2;
          }
    else if (y_1 == y_2 && m_1 < m_2) {
        d += d_2;
    }
    else { }
    
    cout<<d<<" days";
}
Topic archived. No new replies allowed.