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;
}

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.