#include #include // Function to calculate age void calculateAge(int birthYear, int birthMonth, int birthDay, int& ageYears, int&...
#include
#include
// Function to calculate age
void calculateAge(int birthYear, int birthMonth, int birthDay, int& ageYears, int& ageMonths, int& ageDays)
{
// Get the current time
time_t currentTime = time(nullptr);
struct tm* now = localtime(¤tTime);
// Calculate the difference between current year and birth year
ageYears = now->tm_year + 1900 - birthYear;
// Calculate the difference between current month and birth month
ageMonths = now->tm_mon + 1 - birthMonth;
// Calculate the difference between current day and birth day
ageDays = now->tm_mday - birthDay;
// Adjust age if current month and day are before the birth date
if (ageMonths < 0 || (ageMonths == 0 && ageDays < 0))
{
ageYears--;
ageMonths += 12;
}
}
int main()
{
int birthYear, birthMonth, birthDay;
int ageYears, ageMonths, ageDays;
// Input birth date
std::cout << "Enter your birth year: ";
std::cin >> birthYear;
std::cout << "Enter your birth month: ";
std::cin >> birthMonth;
std::cout << "Enter your birth day: ";
std::cin >> birthDay;
// Calculate age
calculateAge(birthYear, birthMonth, birthDay, ageYears, ageMonths, ageDays);
// Output the calculated age
std::cout << "Your age is: " << ageYears << " years, " << ageMonths << " months, and " << ageDays << " days." << std::endl;
return 0;
}