-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCalculateAge.cpp
More file actions
41 lines (30 loc) · 1.03 KB
/
CalculateAge.cpp
File metadata and controls
41 lines (30 loc) · 1.03 KB
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
#include <iostream>
#include <ctime>
using namespace std;
int main() {
int birthYear, birthMonth, birthDay;
// Input: Enter birth year
cout << "Enter your birth year (e.g., 1990): ";
cin >> birthYear;
// Input: Enter birth month
cout << "Enter your birth month (1-12): ";
cin >> birthMonth;
// Input: Enter birth day
cout << "Enter your birth day (1-31): ";
cin >> birthDay;
// Get the current date
time_t currentTime = time(nullptr);
tm* localTime = localtime(¤tTime);
// Calculate the age
int currentYear = localTime->tm_year + 1900;
int currentMonth = localTime->tm_mon + 1;
int currentDay = localTime->tm_mday;
int age = currentYear - birthYear;
// Adjust the age if the birthdate hasn't occurred yet this year
if (currentMonth < birthMonth || (currentMonth == birthMonth && currentDay < birthDay)) {
age--;
}
// Output: Display the calculated age
cout << "Your age is: " << age << " years" << endl;
return 0; // Exit successfully
}