Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab #8 How old are you really? 20 points Objective To show our understanding of

ID: 3820137 • Letter: L

Question

Lab #8 How old are you really? 20 points Objective To show our understanding of functions and specifically how arguments are passed to functions e. pass by value, pass by reference.) To further our understanding of loops, their logic and the correct use of the while0, 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: 1. bool enterDate(int month, int day, int year) 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.

Explanation / Answer

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
int cm,cd,cy,cb,bm,bd,by,nofdaysinmonth; \c for current and b for birth I used short term bcoz of timing issue
bool validDate(int month,int day,int year)
{
int a;
if (month>=1 && month<=12)
{
a=1;
}
else
{
a=0;

}
if(month==4||month==6||month==9||month==11) \for no of days in a month and validating purpose
{
if(day>=1 && day<=30)
{ a=1;

nofdaysinmonth=30;

}
else
{
a=0;
}

}
else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day>=1 && day<=31)
{

a=1;

nofdaysinmonth=31;

}
else
{

a=0;

}

}
}
else if(month==2)
{
if(day>=1 && day<=28)
{ a= 1;

nofdaysinmonth=28; }
else
{ a=0; }
}

return a;

  
}
bool enterDate(int month,int day,int year)
{
int vd;
string date;
cin>>date;
month = stoi(date.substr(0,2));
day = stoi(date.substr(3,2));
year = stoi(date.substr(6));
vd =validDate(month,day,year);
if (cob==0) \for current or todays date
{
cm=month;
cd=day;
cy=year;
}
else if(cob==1) \for birth date
{
bm=month;
bd=day;
by=year;
}
return vd;

}
bool dateBefore(int m1,int d1,int y1,int m2,int d2,int y2)
{

int byvalid;

if(by>cy)
{
byvalid=0;
}
else if(by==cy)
{
if(bm>cm)
{
if(bd>cd)
{
byvalid=0;
}
}
}
else
{
byvalid=1;
}
  
}
}

if (bd==cd && bm==cm) \checking whether today is your birthday

{

cout<<"Happy Birthday";

byvalid=1;

}
return byvalid;

}
bool enterBirthDate(int cmonth,int cday,int cyear,int bmonth,int bday,int byear)
{
int valid,byvalid,vdB;
cout<<"Enter Birth Date";
valid=enterDate(0,0,0); \check whether the format of birth date is correct

if (valid==1)
{
vdB=dateBefore(cm,cd,cy,bm,bd,by);

}

else

{

cout<<"Entered date is invalid";

}

return vdB;

}

void calculateage()

{
int y,m,d;
y=cm-by;
m=12-bm;

d= nofdaysinmonth-bd;

//due to time out I post calculat function in comment section
cout<<"You are "<<y<<"Years"<<m<<"months" <<m <<d<<"days"<<old;

}
int main()
{
int ed,ebd;
char cyn;
cout << "Welcome to Age Calculator! " ;
cout<<"Please enter today's date,Enter Date as mm/dd/yyyy : ";
cob=0; \variable used for the function is called for todays date or birthdate
ed=enterDate(0,0,0); \ Initaly the value is 0 and ed the value return from enterDate()
if (ed==0) \checks for date is valid
{
cout<<"The entered date is invalid,please re-enter,Enter Date as mm/dd/yyyy : ";
}
else if(ed==1)
{
cout<<"Date entered date is" <<cm <<"/"<<cd<<"/"<<cy;
}

cob=1;
ebd=enterBirthDate(cm,cd,cy,0,0,0);
if (ebd==1){

cout<<"would you like to calculate your age (y/n) ?";
cin>>cyn;
if (cyn=='y')
{
calcuateage();
}
if (cyn=='n')
{
cout<<"You are so chikcken";
}
  
}
return 0;
}