0

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;
}
6
Contributors
6
Replies
8
Views
12 Years
Discussion Span
Last Post by The Dude
0

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

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

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.

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.

0

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

0

Excellent program!!

Wish i could do something like that :)

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.