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 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;
#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
constint 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
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 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)
{