fork download
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. int main() {
  5. // Get the current date
  6. time_t now = time(0);
  7. tm *currentTime = localtime(&now);
  8.  
  9. int currentYear = currentTime->tm_year + 1900;
  10. int currentMonth = currentTime->tm_mon + 1; // Months are 0-11
  11. int currentDay = currentTime->tm_mday;
  12.  
  13. // Input birthdate
  14. int birthYear, birthMonth, birthDay;
  15. std::cout << "Enter your birth year (YYYY): ";
  16. std::cin >> birthYear;
  17. std::cout << "Enter your birth month (1-12): ";
  18. std::cin >> birthMonth;
  19. std::cout << "Enter your birth day (1-31): ";
  20. std::cin >> birthDay;
  21.  
  22. // Calculate age
  23. int age = currentYear - birthYear;
  24.  
  25. // Adjust age if the birthday hasn't occurred yet this year
  26. if (currentMonth < birthMonth || (currentMonth == birthMonth && currentDay < birthDay)) {
  27. age--;
  28. }
  29.  
  30. // Output the result
  31. std::cout << "You are " << age << " years old." << std::endl;
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Enter your birth year (YYYY): Enter your birth month (1-12): Enter your birth day (1-31): You are -3182 years old.