The user enters his date of birth and the current date. Count the number of days lived. Where is the mistake?
#include<iostream> using namespace std; int main() { setlocale(LC_ALL, "Russian"); int day1, month1, year1, sum1 = 0, sum2 = 0, count = 0; int day2, month2, year2; int mon[12] = {31, 30, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; cout << "Введите дату рождения: "; cin >> day1 >> "." >> month1 >> "." >> year1; cout << "Введите дату сегоднешнего дня: "; cin >> day2 >> "." >> month2 >> "." >> year2; //////////////////////ПЕРВАЯ ЧАСТЬ for (int i = 0; i < (year1 - 1); i++) { if (i % 4 != 0) { sum1 += 365; } else { sum1 += 366; } } for (int j = 0; j < (month1 - 1); j++) { sum1 += mon[j]; } if (year1 % 4 != 0) { sum1 += day1; } else if ((year1 % 4 == 0) && (month1 > 2)) { sum1 += (day1 + 1); } else { sum1 += day1; } ////////////////////ВТОРАЯ ЧАСТЬ for (int i = 0; i < (year2 - 1); i++) { if (i % 4 != 0) { sum2 += 365; } else { sum2 += 366; } } for (int j = 0; j < (month2 - 1); j++) { sum2 += mon[j]; } if (year2 % 4 != 0) { sum2 += day2; } else if ((year2 % 4 == 0) && (month2 > 2)) { sum2 += (day2 + 1); } else { sum2 += day2; } ///////////////////ВЫВОД cout << "Количество прожитых дней: " << sum2 - sum1 << endl; return 0; }
man 3 mktime) get the dates in seconds . Then just divide the difference in seconds by the length of the day in them (24 * 60 * 60) - avp