Structures: combining data elements

This example uses the time_c struct to display the date and time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Structures: How old
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
struct Person {
  string name;
  int yearBorn;
}; //Person
int currentYear() {
   time_t currentTime=time(0); // get time now
   struct tm *now = localtime(&currentTime); //set values for the structure
   return now->tm_year + 1900;
}
int main() {
   Person student={"Joe",1989};
   int year=currentYear();
   int age=year-student.yearBorn;
   cout<<student.name<<" will be "<<age<<" years old this year.\n";
   system("pause");
   return 0;
} //main
CODE
Output:
Joe will be 28 years old this year.

NEXT: Random Numbers