Starting from:
$30

$24

Homework #1: Calculate a person’s Age: Solution

Age is a derived attribute. Rather than storing a person’s age which changes every year, it is more practical to store a person’s birthdate and calculate the age based on current date and birthdate. 

Tasks to be completed: 
Create an abstract data type age. One of the methods will calculate the age of a person. The data for the code can be stored as either structures or classes. This code can be written as a console application in C++ or similar language to meet the following requirements. 
    1. The program should accept the users input and check for valid/invalid input and warn then the user of any invalid: 
Sample input: 
What is the First Name: Joe 
What is the Last Name: Watson 
What is the birth year: 2004 
What is the birth month: 7 
What is the birth day: 4

    2. Use system time to identify today’s date 
    3. The output should include the person’s name, date of birth, today’s date being used and the person’s age: 
Sample output:
 Joe Watson, age 14 was born on 7-4- 2004 and today is 1-30-2019 
    4. Use a separate file to store the data as structures (or equivalent constructs- e.g., classes would also be a good choice) to store the person’s information. 


The code below is not complete: refer to it if needed. Make sure to organize the code into modules (e.g., methods) also when writing the code, you need to take into account the month and the day. Add validation, and comments to the code. 


#include <iostream>

using namespace std;
struct perosn
{
    char name[20];
    int day;
    int month;
    int year;
..
}person1;


int main()
{
    ..
}


Scenario 
    1. Ask for name and the year, month, and day of birth 
        a. Make sure the entries are valid 
    2. Find current year, month and day 
    3. Find age = current date- person’s birth date 
    4. Display the result 

More products