Objective: To show our understanding of functions and specifically how arguments
ID: 3802793 • Letter: O
Question
Objective: To show our understanding of functions and specifically how arguments are passed to functions (i.e. pass by value, pass by reference.) To further our understanding of loops, their logic and the correct use of the while(), do .. while() and for control structures in the C++ programming language. Specifically to show an understanding of the logically appropriate use of nested loops and embedded conditional statements. To reinforce our understanding of data types and variables. Namely, how to create, initialize, display and perform basic arithmetic operations on those variables.
Assignment: Write a program that calculates the elapsed time between two dates in terms of years, months, and days.
Your program should prompt the user to enter the current date in terms of month, day, year. After the date entered has been validated (as specified below), the program should prompt the user to see if they want to calculate how old they are. If they accept, the program should prompt the user to enter the date of their birth also in terms of month, day, and year. This date also needs to be validated (as specified below). Assuming both days have been correctly validated, the program should then calculate the persons age in years, months, and days and display the information accordingly. If the current date happens to be the person's birthday, your program should output a special congratulations message.
The logic that controls the execution of your program should be in the main() body, however, the program should be designed to accomplish its' objective around at least the following logical functions:
boolenterDate(intmonth,intday,intyear)
This function prompts the user to enter a date (in some specified format as: m d y or m/d/y) and stores the data entered into the three arguments passed. Note you must decide if the arguments to this function should be passed by value or passed by reference. This function should invoke the validDate() function and return true or false false depending on whether the date entered is a valid date.
bool enterBirthDate( int cmonth, int cdate, int cyear, int bmonth, int bday, int byear) This function should invoke the enterDate() function to get the date of the person's birthday. Assuming a valid date was entered, this function should make sure that it is a valid birth date (i.e. date enter occurs before the current date) by invoking the dateBefore() function. This function should return true or false false depending on whether the date entered is a valid birth date. Think carefully which arguments to this function should be passed by reference and which arguments should be passed by value.
3. bool validDate(int month, int day, int year)
This function checks that the month, day and year passed to the function represent a valid date. The function should return the boolean value true or false accordingly. For a date to be valid:
o the month must be between 1 and 12.
o if the month is in {4,6,9,11}, the day must be in {1 .. 30}
o if the month is in {1,3,5,7,8,10,12}, the the day must be in {1 .. 31}
o if the month is in {2}, the day must be in {1 .. 28}. Note, we will ignore leap
years for the moment.
booldateBefore(intmonth1,intday1,intyear1,intmonth2,intday2,intyear2)
This function checks that the date corresponding to the parameters month1, day1, year1 is before the date corresponding to the parameters month2, day2, year2 (i.e. cannot have a birthday if you have not been born yet). The function should return the boolean value true or false accordingly.
voidcalculateAge()
This function calculates your age in years, months, and days. Note: You decide what arguments this function needs to take and how they should be passed. This requires some thought because, as the name of this function indicates, the purpose of this function is to calculate the age in years, months, and days. The display of this information, therefore, should take place after this function returns to wherever it was invoked from. In other words, there should be no cout statements in this function.
To keep the logic simple, assume the following: a year = 365 days (ignore leap years)
a month = 30.5 days
All dates are in this century (2000 onwards).
Following is one possible sample run, but feel free to be creative: Welcome to Age Calculator
Please enter today's date as mm/dd/yyyy:
Response from program is one of:
1. Dateenteredis:mm/dd/yyyy
2. The entered date is invalid, please re-enter, Enter Date as mm/dd/yyyy:
Enter your date of birth, as mm/dd/yyyy:
Assuming a valid birth day has been entered, calculate how old they are and display the results as...
You are Y years, N months and D days old.
Note: Allow the user three attempts only to enter a valid date for each date entered (i.e. current and birth date.) After the 3rd invalid date entered in a row, display a message indicating that three tries have been exhausted, and the program should terminate.
Important
Think through the logic carefully. This is not a typing exercise. If you find yourself repeating blocks of code, stop and re-think the logic of the loops and functions.
The program flow should be controlled by use of conditional logic, there should not be any statement to return or exit the program anywhere in your code.
Each function should use logic to control execution. If a function is expected to return a value (i.e. it is not a void function), there should only be ONE return statement at the end of the function. The body of the function should set the return value accordingly. There should NEVER BE MORE THAN ONE RETURN STATEMENT WITHIN THE BODY OF A FUNCTION!
Follow an incremental approach in writing your program. In other words, write a few lines of code, make sure it compiles and executes as expected, then continue with more code.
If the program compiles but does not work as expected, consider using cout statements to display the value of variables in order to identify the errors.
For full credit be sure to:
Include a descriptive Comment Block.
Use correct indentation and allignment.
Use descriptive identifiers for variable names as well as appropriate data types.
Use blank lines to separate the code into appropriate blocks.
Include comments to help others understand your program, and help yourself
think!
Explanation / Answer
Hello i have given the code in c++
#include<<iostream>
// structure for month , date and year//
struct Date {
int month;
int day;
int year;
// Member functions
Date get_date();
Date get_birth_date();
bool is_valid_date(int year, int month, int day);
bool is_before(Date& date1, Date& date2);
Date is_birthday(Date& date1, Date& date2);
Date calculate_age(Date& date1,Date& date2);
};
// Declaration
Date get_date();
Date get_birth_date();
bool is_valid_date(int year, int month, int day);
bool is_before(Date& date1, Date& date2);
Date calculate_age(Date& date1,Date& date2);
int main()
{
int i=0, j=0;
Date date1 = get_date();
Date date2 = get_birth_date();
Date get_date();
// Check if the date is correct
if (! is_valid_date(year, month, day))
error("Date is not valid.");
Date get_birth_date();
// Is birthday valid as well?
if (! is_valid_date(int year, int month, int day))
error("Date is not valid.");
// Is the birthday before 'today'?
if (!(is_before(Date& date1, Date& date2)))
error("Your birthday should not be later than today.");
// Calculate and give the answer.
Date calculate_age();
}
Date get_date()
{
Date date1;
if(i<3)
{
cout << "Welcome to the age calculator! ";
cout << "Please enter today's date (mm/dd/yyyy): ";
int m;
int d;
int y;
char slash; // To ignore the '/' during reading
cin >> m;
date1.month = m;
cin >> slash;
cin >> d;
date1.day = d;
cin >> slash;
cin >> y;
date1.year = y;
cout << "Date entered was "<< date1.month << "/" << date1.day << "/" << date1.year <<" ";
}
else
{
Cout<<” Three invalid date entered!!!! Your attempt is over”;
exit(0);
}
i++;
}
Date get_birth_date()
{
if(j<3)
{
Date date2;
cout << "Please enter your birth date (mm/dd/yyyy): ";
int m;
int d;
int y;
// To ignore the '/' during reading
char slash;
cin >> m;
date2.month = m;
cin >> slash;
cin >> d;
date2.day = d;
cin >> slash;
cin >> y;
date2.year = y;
cout << "Your birthday is "<< date2.month << "/" << date2.day << "/" << date2.year <<" ";
}
else
{
Cout<<” Three invalid date entered!!!! Your attempt is over”;
exit(0);
}
j++;
}
// Is date valid?
bool is_vaild_date(int year, int month, int day)
{
if (day<0)
return false;
if (month<1 || month>12)
return false;
// Check if the date exists or not considering there are have
// different days when month varies
int days_in_month=31;
switch (month){
case 2:
days_in_month=28;
break;
case 4: case 6: case 9: case 11:
days_in_month=30;
break;
}
if (days_in_month<day)
return false;
return true;
}
/Date check - is birthday 'today'?
Date is_birthday(Date& date1, Date& date2)
{
If( (Date& date1.year=Date& date2.year)&& (Date& date1.month<Date& date2.month)&&(Date& date1.day< Date& date2.day)
cout<<”Happy Birthday!!! Wish You a long life!!”;
}
/Date check - is birthday before 'today'?
bool is_before(Date& date1, Date& date2)
{
if (Date& date1.year<Date& date2.year)
return false;
else if (Date& date1.month<Date& date2.month)
return false;
else if (Date& date1.day< Date& date2.day)
return false;
return true;
}
// Calculation
Date calculate_age(Date& date1,Date& date2)
{
Date date3;
date3.day = date1.day - date2.day;
date3.month = date1.month - date2.month;
date3.year = date1.year - date2.month;
if (date3.day<0){
date3.day = 30 + date3.day;
date3.month = date3.month - 1;
}
if (date3.month<0){
date3.month = 12 + date3.day;
date3.year = date3.year - 1;
}
cout <<"You are " << date3.year <<" years, "<< date3.month <<" months, and " << date3.day <<" days old. ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.