Write a program that asks for the users name and year of birth, greets the user
ID: 3772093 • Letter: W
Question
Write a program that asks for the users name and year of birth, greets the user by name, and declares the users age in years. Users are assumed to be born between the years 1800 and 2099, and should enter the year of birth in one of the three formats 18XX, 19XX, or 20XX. A typical output should be: Hello Caroline, you are 23 years old. -This program should work regardless of the current year at the time of its use. Therefore, you will not set the year value to whatever the current year is now. Instead, you will let the computer select the current year by using the time function (you’ll need to include) and &epoch. You should also use the localtime function as well.(C++)
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<ctime.h>
void main()
{
int year;
char name[20];
cout<<"Enter your name";
cin>>name;
cout<<"Enter your birth year [yyyy]";
cin>>year;
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
age = now->tm_year - year;
cout << "Hello "<<name <<", you are
"<<age<<" year old";
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.