I am supposed to be writing a program to type in the current date and birth date and test whether the dates typed in are valid and then calculate the age. However, no matter what I typed in for birth date, it always invalid. Please help me locating where I did wrong and correct it if possible. Thank you so much!
#include <iostream>
using namespace std;
int main(){
int mm,dd,yy;
int bm,bd,by;
int year,month,day;
void getDate(int &mm,int &dd,int &yy);
bool validDate( int yy, int dd, int mm );
bool dateBefore( int dd, int mm, int yy, int bd, int bm, int by);
bool validBirthDate (int bd, int bm, int by, int dd, int mm, int yy);
void calculateAge (int dd, int mm, int yy,int bd, int bm, int by, int &year, int &month, int &day);
bool valid;
cout <<"Welcome to age calculator!";
cout << "Please Enter today's Date: ";
getDate( mm, dd, yy );
valid = validDate( mm, dd, yy );
if ( valid )
cout << "date is valid\n";
else
cout << "date isnt valid\n";
cout << "Please Enter Birth Date: ";
getDate(bm, bd, by);
dateBefore ( dd,mm,yy,bd,bm,by);
valid = validBirthDate(mm, bm, dd, bd, yy, by );
if ( valid )
cout << "birthdate is valid\n";
else
cout << "birthdate isnt valid\n";
calculateAge(mm,dd,yy,bm,bd,by,year,month,day);
cout <<"Your are "<<year<<"years "<<month<<"months and "<<day<<"days old";
}
void getDate( int &mm, int &dd, int &yy ) {
cin >> mm >> dd >> yy;
cout << "Date entered: " << mm << ' ' << dd << ' '<< yy << endl;
}
bool validDate( int mm, int dd, int yy ) {
bool status = true;
if (yy>2015)
status=false;
else{
switch(mm){
case 1:
if (dd>=1&&dd<=31)
status=true;
else
status= false;
break;
case 2:
if (dd>=1&&dd<=28)
status=true;
else
status=false;
case 3:
if (dd>=1&&dd<=31)
status=true;
else
status= false;
break;
case 4:
if (dd>=1&&dd<=30)
status=true;
else
status=false;
break;
case 5:
if (dd>=1&&dd<=31)
status=true;
else
status=false;
break;
case 6:
if (dd>=1&&dd<=30)
status=true;
else
status=false;
break;
case 7:
if (dd>=1&&dd<=31)
status=true;
else
status=false;
break;
case 8:
if (dd>=1&&dd<=31)
status= true;
else
status= false;
break;
case 9:
if (dd>=1&&dd<=30)
status=true;
else
status=false;
break;
case 10:
if (dd>=1&&dd<=31)
status=true;
else
status=false;
break;
case 11:
if (dd>=1&&dd<=30)
status=true;
else
status=false;
break;
case 12:
if (dd>=1&&dd<=31)
status=true;
else
status=false;
break;
default:
status=false;
}
return( status );
}
}
bool dateBefore(int mm, int dd, int yy, int bm, int bd, int by) {
bool status=true;
if (by-yy>0)
status=false;
if (by==yy&&bm==mm&&bd==dd)
status=false;
if (by==yy&&bm==mm&&bd>dd)
status=false;
if (by==yy&&bm>mm)
status=false;
else
status=true;
return (status);
}
bool validBirthDate (int mm, int dd, int yy, int bm, int bd, int by) {
bool status=true;
if (validDate(bm,bd,by)&&dateBefore(bm,bd,by,mm,dd,yy))
status= true;
else
status=false;
return (status);
}
void calculateAge(int mm, int dd, int yy, int bm, int bd, int by,int &year, int &month, int &day)
{
year=yy-by;
month=mm-bm;
if (bm>mm)
{ year=yy-by-1;
month=mm+12-bm;
}
day=dd-bd;
if (bd>dd)
{ month=month-1;
day=dd-bd+30;
}
}