# include # include # include using namespace std; int main ( ) // // DAY_COUNT counts the number of days from some point in the past to today. // // Using this program, you can find out how many days old you are. // { int y, m, d; int y1, m1, d1; int y2, m2, d2; int days; int month_length ( int m, int y ); int now_year ( ); int now_month ( ); int now_day ( ); cout << "Enter Year Month Day as integers: "; cin >> y1 >> m1 >> d1; y2 = now_year ( ); m2 = now_month ( ); d2 = now_day ( ); cout << " Then = " << y1 << "/" << m1 << "/" << d1 << "\n"; cout << " Today = " << y2 << "/" << m2 << "/" << d2 << "\n"; days = 0; y = y1; m = m1; d = d1; while ( y < y2 || m < m2 || d < d2 ) { days = days + 1; d = d + 1; if ( month_length ( m, y ) < d ) { d = 1; m = m + 1; } if ( 12 < m ) { m = 1; y = y + 1; } } cout << " Number of days is " << days << "\n"; return 0; } int month_length ( int m, int y ) // // MONTH_LENGTH returns the length of the month M, in year Y. // { bool leap_year ( int y ); int value; if ( m == 4 || m == 6 || m == 9 || m == 11 ) { value = 30; } else if ( m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 ) { value = 31; } else { if ( leap_year ( y ) ) { value = 29; } else { value = 28; } } return value; } bool leap_year ( int y ) // // LEAP_YEAR returns TRUE if the year Y was a leap year. // { bool value; if ( y % 4 != 0 ) { value = false; } else if ( y % 400 == 0 ) { value = true; } else if ( y % 100 == 0 ) { value = false; } else { value = true; } return value; } int now_day ( ) // // NOW_DAY returns the day of the month for today. // { const struct tm *tm_ptr; time_t now; int value; now = time ( 0 ); tm_ptr = localtime ( &now ); value = tm_ptr->tm_mday; return value; } int now_month ( ) // // NOW_MONTH returns the month number for today. // { const struct tm *tm_ptr; time_t now; int value; now = time ( 0 ); tm_ptr = localtime ( &now ); value = 1 + tm_ptr->tm_mon; return value; } int now_year ( ) // // NOW_YEAR returns the year for today. // { const struct tm *tm_ptr; time_t now; int value; now = time ( 0 ); tm_ptr = localtime ( &now ); value = 1900 + tm_ptr->tm_year; return value; }