writing a program to calculate age?

I'm trying to write a program that will calculate the age of the user assuming the birthdate is after 2000. However, all the program prints is the date that the user entered. I've been working on this all day, and I still haven't found a solution to my problem. Here's my code now:

#include <iostream>
#include <iomanip>
using namespace std;

bool enterDate(int& month, int& day, int& year);
bool enterBirthDate(int cmonth, int cday, int cyear, int& bmonth, int& bday, int& byear);
bool validDate(int& month, int& day, int& year);
bool dateBefore(int month1, int day1, int year1, int month2, int day2, int year2);
void calculateAge(int cmonth, int cday, int cyear, int bmonth, int bday, int byear);

int main()
{
int month, day, year;
char userInput;

cout << "Welcome to the Age Calculator" << endl;
cout << "Please enter today's date as mm dd yyyy: " << endl;

bool validDate;
do
{
validDate = enterDate(month, day, year);
if(!validDate)
{
cout << "Invalid date" << endl;
cout << "Please re-enter date: " << endl;
}
}while(!validDate);

if (validDate == false)
{
cout << "Goodbye" << endl;
}

else if (validDate == true)
{
cout << "Do you want to calculate your age? Enter y/n: " << endl;
cin >> userInput;
}

if(userInput == 'n' || userInput == 'N')
cout << "Goodbye" << endl;
if(userInput == 'y' || userInput == 'Y')
{
int bmonth, bday, byear;
do
{
validDate = enterBirthDate(month, day, year, bmonth, bday, byear);
if(!validDate)
{
cout << "Invalid date" << endl;
cout << "Please re-enter date:" << endl;
}
} while (!validDate);

do
{
if(dateBefore(month, day, year, bmonth, bday, byear) == false)
{
cout << "You cannot be born in the future" << endl;
enterBirthDate(month, day, year, bmonth, bday, byear);
}
} while ((dateBefore(month, day, year, bmonth, bday, byear)) == false);

int cmonth, cday, cyear, dmonths, ddays, dyears;
calculateAge(cmonth, cday, cyear, bmonth, bday, byear);
}
}

bool enterDate(int& month, int& day, int& year)
{
cin >> month >> day >> year;
cout << "Date entered is: " << month << "/" << day << "/" << year << endl;
return (validDate(month, day, year));
}

bool enterBirthDate(int cmonth, int cday, int cyear, int& bmonth, int& bday, int& byear)
{
int month, day, year;
cout << "Please enter your birth date as mm dd yyyy: " << endl;
enterDate(bmonth, bday, byear);
dateBefore(month, day, year, bmonth, bday, byear);
return (validDate(bmonth, bday, byear));
}

bool validDate(int& month, int& day, int& year)
{
bool valid;
while ((month <= 12 && month >= 1 && year >= 2000)) {
if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >= 1 && day <= 30))
valid = true;
else if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day >= 1 && day <= 31))
valid = true;
else if ((month == 2 && (day >= 1 && day <= 28)))
valid = true;
else
valid = false;
}
return valid;
}

bool dateBefore(int month1, int day1, int year1, int month2, int day2, int year2)
{
bool after;
if (year2 < year1)
after = true;
else if (year2 == year1 && month2 < month1)
after = true;
else if (year2 == year1 && month2 == month1 && day2 < day1)
after = true;
else
after = false;
return after;
}

void calculateAge(int cmonth, int cday, int cyear, int bmonth, int bday, int byear)
{
int dyears = cyear - byear;
int dmonths, ddays;
if (cmonth > bmonth)
{
dmonths = cmonth - bmonth;
}
else if (cmonth < bmonth)
{
dmonths = 12 - bmonth + cmonth;
dyears = dyears - 1;
}
else if (cmonth = bmonth)
{
dmonths = 0;
}
ddays = 0;
if (cday < bday)
{
if (cmonth == 01)
{
ddays = ddays + 31;
}
else if (cmonth == 02)
{
ddays = ddays + 28;
}
}
cout << "You are " << dyears << " years old, " << dmonths << " months old, and " << ddays << " days old." << endl;

if (dyears == 0 && dmonths == 0 && ddays == 0)
{
cout << "Happy birthday!" << endl;
}
}

I would also appreciate it if you guys check to make sure the code is correct. Thank you for all your help!
if (cmonth = bmonth)
this is always true and is an assignment statement. Are there others like it?
closed account (48T7M4Gy)
This another way of calculating the age. (Note: It doesn't adjust for leap years and months go from 0 to 11, but they are fairly easy to fix up.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

int main()
{
    int birth_year = 1986;
    int birth_month = 9;
    int birth_day = 15;
    
    int now_year = 2017;
    int now_month = 3;
    int now_day = 27;
    
    // SETUP CALENDAR - modify later for leap years
    int days_per_month[]{ 31,28,31,30,31,30,31,31,30,31,30,31 };
    
    // SETUP DAY NUMBERS FOR YEAR
    const int MONTHS = 12;
    int cumulative_days[MONTHS]{0};
    int sum = 0;
    for(int i = 0; i < MONTHS; ++i){
        sum += days_per_month[i];
        cumulative_days[i] = sum;
    }
    
    std::cout << "Enter birth date <year month day>: ";
    std::cin >> birth_year >> birth_month >> birth_day;
    
    // CALCULATE FULL YEARS
    int full_years = now_year - birth_year - 1;
    std::cout << "Full years: " << full_years << '\n';
    
    
    // CALCULATE DAYS FROM START OF TODAYS DATE
    int days_this_year = 0;
    days_this_year = now_day + cumulative_days[now_month - 1];
    
    
    // CALCULATE DAYS FROM BIRTH DATE TO END OF THAT YEAR
    int days_birth_year = 0;
    days_birth_year = cumulative_days[11] - ( birth_day + cumulative_days[birth_month - 1] ) ;
    
    int total_days = days_this_year + days_birth_year;
    
    double age = full_years + total_days/365.0;
    std::cout << "Age: " << age << '\n';
    
    
    return 0;
}


Enter birth date <year month day>: 1983 05 22
Full years: 33
Age: 33.8466
Program ended with exit code: 0


Enter birth date <year month day>: 2016 03 27
Full years: 0
Age: 1
Program ended with exit code: 0
Last edited on
I should have mentioned this before, but my instructor does not want us to use arrays. However, I fixed most of the problems- it turned out that I shouldn't have used a while loop because it would repeat infinitely, so I replaced it with an if statement. Now, everything works, but the only problem is that ddays is not printing correctly, it just prints 0. Here is my improved code:

#include <iostream>
#include <iomanip>
using namespace std;

bool enterDate(int& month, int& day, int& year);
bool enterBirthDate(int cmonth, int cday, int cyear, int& bmonth, int& bday, int& byear);
bool validDate(int& month, int& day, int& year);
bool dateBefore(int month1, int day1, int year1, int month2, int day2, int year2);
void calculateAge(int cmonth, int cday, int cyear, int bmonth, int bday, int byear, int& dmonth, int& dday, int& dyear);

int main()
{
int month, day, year;
int bmonth, bday, byear;
int dmonth, dday, dyear;
char userInput;

cout << "Welcome to the Age Calculator" << endl;
cout << "Please enter today's date as mm dd yyyy: " << endl;

bool validDate;
do
{
validDate = enterDate(month, day, year);
if(!validDate)
{
cout << "Invalid date" << endl;
cout << "Please re-enter date: " << endl;
}
}while(!validDate);

if (validDate == false)
{
cout << "Goodbye" << endl;
}

else if (validDate == true)
{
cout << "Do you want to calculate your age? Enter y/n: " << endl;
cin >> userInput;
}

if(userInput == 'n' || userInput == 'N')
cout << "Goodbye" << endl;

if(userInput == 'y' || userInput == 'Y')
{

do
{
validDate = enterBirthDate(month, day, year, bmonth, bday, byear);
if(!validDate)
{
cout << "Invalid date" << endl;
cout << "Please re-enter date:" << endl;
}
} while (!validDate);

do
{
if(dateBefore(month, day, year, bmonth, bday, byear) == false)
{
cout << "You cannot be born in the future" << endl;
enterBirthDate(month, day, year, bmonth, bday, byear);
}
} while ((dateBefore(month, day, year, bmonth, bday, byear)) == false);


calculateAge(month, day, year, bmonth, bday, byear, dmonth, dday, dyear);
}
}

bool enterDate(int& month, int& day, int& year)
{

cin >> month >> day >> year;
cout << "Date entered is: " << month << "/" << day << "/" << year << endl;
bool isValid =(validDate(month, day, year));

return isValid;
}

bool enterBirthDate(int cmonth, int cday, int cyear, int& bmonth, int& bday, int& byear)
{
int month, day, year;
cout << "Please enter your birth date as mm dd yyyy: " << endl;
enterDate(bmonth, bday, byear);
dateBefore(month, day, year, bmonth, bday, byear);
return (validDate(bmonth, bday, byear));
}

bool validDate(int& month, int& day, int& year)
{
bool valid;
if ((month <= 12 && month >= 1 && year >= 2000)) {
if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >= 1 && day <= 30))
valid = true;
else if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day >= 1 && day <= 31))
valid = true;
else if ((month == 2 && (day >= 1 && day <= 28)))
valid = true;
else
valid = false;

}
return valid;
}

bool dateBefore(int month1, int day1, int year1, int month2, int day2, int year2)
{
bool after;
if (year2 < year1)
after = true;
else if (year2 == year1 && month2 < month1)
after = true;
else if (year2 == year1 && month2 == month1 && day2 < day1)
after = true;
else
after = false;
return after;
}

void calculateAge(int cmonth, int cday, int cyear, int bmonth, int bday, int byear, int& dmonth, int& dday, int& dyear)
{

dyear = (cyear - byear);

if (cmonth > bmonth)
{
dmonth = cmonth - bmonth;
}
else if (cmonth < bmonth)
{
dmonth = 12 - bmonth + cmonth;
dyear = dyear - 1;

}
else if (cmonth == bmonth)
{
dmonth = 0;
}

if (cday < bday)
{
if (cmonth == 01 || cmonth == 3 || cmonth == 5 || cmonth == 7 || cmonth == 8 || cmonth == 10 || cmonth == 12)
{
dday = dday + 31;

}
else if (cmonth == 02)
{
dday = dday + 28;

}
else if (cmonth == 4 || cmonth == 6 || cmonth == 9 || cmonth == 11)
dday = dday + 30;

}
cout << "You are " << dyear << " years old, " << dmonth << " months old, and " << dday << " days old." << endl;

if (dmonth == 0 && dday == 0)
{
cout << "Happy birthday!" << endl;
}
}

Thank you again for all your help!
I found out the problem! I forgot to write the formula for dday if cday > bday. It works now, thanks to everyone who helped!
Topic archived. No new replies allowed.