954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

age calculator

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;
}
phr0stbyt3
Newbie Poster
16 posts since Oct 2004
Reputation Points: 10
Solved Threads: 1
 

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;
}
phr0stbyt3
Newbie Poster
16 posts since Oct 2004
Reputation Points: 10
Solved Threads: 1
 

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

dk40k
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

sara
Newbie Poster
10 posts since Apr 2005
Reputation Points: 11
Solved Threads: 0
 

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.

MarkKnutson
Newbie Poster
14 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

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

arit
Newbie Poster
5 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Excellent program!!

Wish i could do something like that :)

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

The Dude
Nearly a Senior Poster
3,485 posts since Dec 2005
Reputation Points: 1,054
Solved Threads: 31
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You