jeudi 19 mars 2015

How to calculate the age of someone from their birthdate C++


Hi all i want to know how i would calculate someones age based on the info they input i.e they enter what date it is for them at the time (yy/mm/dd) then they enter their birthdate (yy/mm/dd) and then the program gives them their age in years, months and days. I have managed to get it to tell the use how many years plus months old they are but cant figure out how to get the days to work correctly (another problem with days is there is different days with different months). Here is what i have so far:



#include <iostream>

using namespace std;

int main()
{
int yearNow;
int monthNow;
int yearBorn;
int monthBorn;
int numberOfMonths=0;
int dayNow;
int dayBorn;
int numberOfDays=0;

char endFlag = 'n';
while (endFlag != 'y'){

cout<<"Which year is it now?"<<endl;
cin>>yearNow;

cout<<"Which month is it now? (please write in number form)"<<endl;
cin>>monthNow;

cout<<"Which day is it now? (please write in number form)"<<endl;
cin>>dayNow;

cout<<"Which year where you born in?"<<endl;
cin>>yearBorn;

cout<<"Which month where you born in? (please write in number form)"<<endl;
cin>>monthBorn;

cout<<"Which day where you born on? (please write in number form)"<<endl;
cin>>dayBorn;

if(monthBorn>monthNow){
yearBorn=yearBorn+1;
}
if(monthBorn==monthNow){
numberOfMonths=0;
}

while(monthBorn!=monthNow){
if(monthBorn>12){
monthBorn=1;
continue;
}
monthBorn++;
numberOfMonths++;
}

while(dayBorn!=dayNow){
if(dayBorn>28){

}
dayBorn++;
numberOfDays++;
}

cout<<"You are "<<yearNow-yearBorn<<" years and "<<numberOfMonths<<" months and "<<numberOfDays<<" days old."<<endl;

cout << "Are you done, Y/N \n";
cin >> endFlag;

}

return 0;
}


Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire