im writing a program that calculates the users age when the current date is entered and the birth date is entered ive been thinking for about 2 hours and i cant figure out how to calculate the age this is what i have so far, im stuck at calcYears cuz im so tired from staying up till 4am last night finishing the WRONG program... sigh :(

#include <iostream>
using namespace std;

void getDate(int&, int&, int&);
void getData(int&, int&, int&);
int calcYears(int, int);

int main()
{
    int month, day, year, currentMonth, currentDay, currentYear, years;
    getDate(currentMonth, currentDay, currentYear);
    getData(month, day, year);
    years = calcYears(year, currentYear);
    
    cout << "\t" << years;
    //cout << "\t" << month << "\t" << day << "\t" << year << endl;
    //cout << "\t" << currentMonth << "\t" << currentDay << "\t" <<currentYear << endl;
    system("pause");
    return 0;
}

void getData(int& month, int& day, int& year)
{
    cout << "\nEnter the month you were born on: ";
    cin >> month;
    cout << "\nEnter the day you were born: ";
    cin >> day;
    cout << "\nEnter the year you were born: ";
    cin >> year;
    cout << endl;
    
    return;
}    

void getDate(int& currentMonth, int& currentDay, int& currentYear)
{
    cout << "\nEnter the current month: ";
    cin >> currentMonth;
    cout << "\nEnter the current day: ";
    cin >> currentDay;
    cout << "\nEnter the current year: ";
    cin >> currentYear;
    cout << endl;
    
    return;
}    
int calcYears(int year, int currentYear, int month, int currentMonth, int day, int currentDay)
{
    int years;
    
    years = currentYear - year;
    if (month >= currentMonth && days > ...
    return years;
}

Dani AI

Generated

’s approach was on the right track: compute the raw year difference and then decide whether the birthday has already occurred this year. Two common pitfalls from the thread are (1) mixing up parameter order when calling the function and (2) using an overly strict condition that requires both month and day comparisons to be true (the classic “&&” vs “||” trap). correctly pointed out the proper birthday check; the intent is to subtract one year only when the current date is strictly before the birthday this year.

A minimal, clear implementation that separates concerns:

int computeAge(int birthY, int birthM, int birthD,
               int curY, int curM, int curD)
{
    int age = curY - birthY;
    if (curM < birthM || (curM == birthM && curD < birthD))
        --age;
    return age;
}

Quick test cases to verify boundaries:

  • Birth 1990-06-15, Current 2020-06-14 -> age = 29 (birthday not yet reached)
  • Birth 1990-06-15, Current 2020-06-15 -> age = 30 (birthday today)
  • Birth 1990-06-15, Current 2020-06-16 -> age = 30 (birthday passed)

Notes and troubleshooting: validate inputs (month 1–12, day within month; handle Feb 29 via leap-year check). Feb 29 birthdays are ambiguous in non-leap years—choose a policy (treat as Feb 28 or Mar 1) and document it. For production code, prefer library types (C++20’s chrono year_month_day or platform Date/Time APIs) for validation and clarity; this echoes ’s suggestion about using built-in date/time tools.

Recommended Answers

All 6 Replies

guess i just needed to take a break :)

#include <iostream>
using namespace std;

void getDate(int&, int&, int&);
void getData(int&, int&, int&);
int calcYears(int, int, int, int, int, int);

int main()
{
    int month, day, year, currentMonth, currentDay, currentYear, age;
    getDate(currentMonth, currentDay, currentYear);
    getData(month, day, year);
    age = calcYears(year, currentYear, month, currentMonth, day, currentDay);
    
    cout << "\t" << age;
    //cout << "\t" << month << "\t" << day << "\t" << year << endl;
    //cout << "\t" << currentMonth << "\t" << currentDay << "\t" <<currentYear << endl;
    system("pause");
    return 0;
}

void getData(int& month, int& day, int& year)
{
    cout << "\nEnter the month you were born on: ";
    cin >> month;
    cout << "\nEnter the day you were born: ";
    cin >> day;
    cout << "\nEnter the year you were born: ";
    cin >> year;
    cout << endl;
    
    return;
}    

void getDate(int& currentMonth, int& currentDay, int& currentYear)
{
    cout << "\nEnter the current month: ";
    cin >> currentMonth;
    cout << "\nEnter the current day: ";
    cin >> currentDay;
    cout << "\nEnter the current year: ";
    cin >> currentYear;
    cout << endl;
    
    return;
}    
int calcYears(int year, int currentYear, int month, int currentMonth, int day, int currentDay)
{
    int years;
    
    years = currentYear - year;
    if (month >= currentMonth && day >= currentDay)
        years = years - 1;
    
    return years;
}

Nice programming, what was the overall time it took for you to finish that? Was it some sort of school project..

hi..
can u help me calculate age using vb.net.
i have txtage.text and txtdob.text.
the format dob is 21.03.1978.

and if txtage.text <35 it will in group 1, if txtage.text < 49 it will in group 2. the group in radiobutton. i've bee thinking this problem almost 3 days. please anyone out there help me.

The code sample above looks about right.

I just remembered, .net has a timespan object does this--among other things. You will need to verify that the month and day are taken into account with it as well.

For programming if you still can't figure it out, write down two dates and figure it out and then remember how you figured it out and code that. If you can't figure it out on paper with two dates, you surely will not be able to code it.

Be sure to test it with specially selected dates to make sure your ands and greater thans are done properly.

Hi Guys,
The code above has a mistake in the most important line of code:

if (month >= currentMonth && day >= currentDay)

it must be replaced by this:

if ((month > crtMonth) || ( month == crtMonth && day > crtDay))

The rest is fine.

Regards
arit

Excellent program!!

Wish i could do something like that :)

I have done some simple HTML scripts,but nothing like that!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.