# Calculating age with a class Date[defining function problem]

• (2 Pages)
• 1
• 2

## 15 Replies - 20982 Views - Last Post: 27 May 2008 - 09:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=52973&amp;s=4b44fb9a7e8bf7c7177f903b8676ff7e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Izumi

Reputation: 0
• Posts: 83
• Joined: 13-March 08

# Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 07:54 AM

I've created a class that let one set the current date, write in their birthday and I want to get a function that will allow to calculate the person's age using the two dates.
The codes I've written so far are the following:

```#include <iostream>
#include <Cstring>

using namespace std;

class Date
{
private:
int day,month,year;//the current date.
public:

int d_birth,m_birth,y_birth;//date of birth..
bool validDate(int d, int m, int y)
{
if (d < 1 || d > 31) return 0;
if (m < 1 || m > 12) return 0;
if (y < 1900) return 0;

return 1;
}
Date(int d,int m,int y)
{if (validDate(d, m, y))
{day=d;
month=m;
year=y;//the current day..
}
else cout<<"ERROR!! No such date. PLEASE Re-Start PROGRAM!";}

void setDate()
{
cout<<"enter ur birth day:";
cin>>d_birth;

if (d_birth<0 || d_birth>31)
cout<<"enter ur birth day agin please:";
else cout<<"enter ur birth month:";
cin>>m_birth;

if (m_birth<0 || d_birth>12)
cout<<"enter ur birth month agin please:";
else cout<<"enter ur birth year:";
cin>>y_birth;

}

void age()
{
//I don't know how to do the calculation here - this is where my problem is

}

};

int main()
{ int x,y,z;
cout<<"What is today's date? d/m/year:";
cin>>x>>y>>z;
Date currnt(x,y,z);
int s;
do
{
cout<<"2-Calculate age"<<endl;
cout<<"choose One of this menu or '-1' to exit:"<<endl;
cin>>s;

switch(s)
{
case 1:
currnt.setDate();
break;

case 2:
currnt.age();
break;

case -1:
break;

}
}while (s != -1);

return 0;
}

```

As you can see I have a small menu to navigate it easier.
My other question is - how can I stop the menu appearing if my bool function is 0 and have the program go back to the 'enter today's date' part of the program on its own instead of my current 'Please restart the program' x'D?

Is This A Good Question/Topic? 0

## Replies To: Calculating age with a class Date[defining function problem]

### #2 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 03:36 PM

um... so your questions is basically:

"How can I conditionally control the flow of my program based upon the output of a boolean function?"

um, generally the answer to that is: By using conditional flow control structures. if, if-else, select-case, for-loop, while-loop, do-while loop...

There are many many many ways that you can use/combine the various structures to form a solution. The main purpose to taking a programming class is to learn how to do just that... i.e. I can't do your homework for you (even if it is stuff you should already know... especially if it is stuff you should already know).

I can say that part of the idea of modular programming is to separate out concerns. Putting cout/cin inside of a "Date" class is really poor OOP design (because user input and output has nothing to do with Dates, or more accurately, Dates should have nothing to do with user input/output). So if I were writing your program I may write a function to control the input of a data, and I would pack all the logic needed to coax a proper date out of the user into that function. I would then call that function from the menu and only return form that function when the date entered was valid.

### #3 Izumi

Reputation: 0
• Posts: 83
• Joined: 13-March 08

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 03:42 PM

Mmm, no. My question is how to define the following function
```void age()
{
//I don't know how to do the calculation here - this is where my problem is

}

```

so that it will calculate the age by having the date and the birthday date input by the user.

P.S. those programs are not for homework, I'm studying c/c++ on my own in between my highschool final exams. No class taken. If I was taking a class I would have had someone to ask, no? *sighs*

### #4 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 04:20 PM

The Date class is always a fun discussion. (too bad sarcasm does not really translate well into typing).

The problem with representing dates as year, month, day is that different months have different numbers of days, and different years have different numbers of days (because of leap years). This makes finding the exact difference between dates a little tricky.

Generally to simplify many operations Date/times are represented as the number of milliseconds that have passed from a given reference point. Then many of the operations (addition, subtraction, etc) can be done with simple arithmetic, and then a pair of functions to do the coversion to/from this number of milliseconds.

For what you are doing it would be enough to just use number of days since some reference point.

Try to get some of the other functions written for manipulating dates first. For example Add(Date someday, int NumDays). and Subtract(Date someday, int NumDays).

### #5 Izumi

Reputation: 0
• Posts: 83
• Joined: 13-March 08

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 04:27 PM

I'm not really sure if I understood what you said [sorry English is not my native and being 2.20am makes me quite slow]. But the exact date as in hours minutes seconds, etc etc etc is not really needed for just a normal calculating person's age, no? Because even if the person is born on a leap year they still get older with every next year. Plus I'm seeking something simpler - it would be enough to have a function to determine how much is currentyear-birthdayyear=? and if the birthday month and day are after the current year's month and day.

### #6 baavgai

• Dreaming Coder

Reputation: 6378
• Posts: 13,625
• Joined: 16-October 07

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 04:28 PM

Izumi, on 25 May, 2008 - 06:42 PM, said:

Mmm, no. My question is how to define the following function
```void age()
{
//I don't know how to do the calculation here - this is where my problem is

}

```

You need two values to figure this out. You need to compare one thing to the other. Here's the idea. I'm not 100% on the logic, it probably needs to flip flop for negatives. I'll leave it to you to worry it out.

```class Date {
private:
int day,month,year;//the current date.
public:
int getYearDifference(Date & dOther) {
int years = dOther.year - this->year;
if (this->month > dOther.month || (this->month == dOther.month && this->day == dOther.day) ) {
years++;
}
return years;
}
}

```

Hope this helps.

### #7 Izumi

Reputation: 0
• Posts: 83
• Joined: 13-March 08

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 04:39 PM

When I use

```#include <iostream>
#include <Cstring>

using namespace std;

class Date
{
private:
int day,month,year;//the current date.
public:

int d_birth,m_birth,y_birth;//date of birth..
bool validDate(int d, int m, int y)
{
if (d < 1 || d > 31) return 0;
if (m < 1 || m > 12) return 0;
if (y < 1900) return 0;

return 1;
}
Date(int d,int m,int y)
{if (validDate(d, m, y))
{day=d;
month=m;
year=y;//the current day..
}
else cout<<"ERROR!! No such date. PLEASE Re-Start PROGRAM!";}

void setDate()
{
cout<<"enter ur birth day:";
cin>>d_birth;

if (d_birth<0 || d_birth>31)
cout<<"enter ur birth day agin please:";
else cout<<"enter ur birth month:";
cin>>m_birth;

if (m_birth<0 || d_birth>12)
cout<<"enter ur birth month agin please:";
else cout<<"enter ur birth year:";
cin>>y_birth;

}

int getYearDifference(Date & dOther) {
int years = dOther.year - this->year;
if (this->month > dOther.month || (this->month == dOther.month && this->day == dOther.day) ) {
years++;
}
return years;
}

};

int main()
{ int x,y,z;
cout<<"What is today's date? d/m/year:";
cin>>x>>y>>z;
Date currnt(x,y,z);
int s;
do
{
cout<<"2-Calculate age"<<endl;
cout<<"choose One of this menu or '-1' to exit:"<<endl;
cin>>s;

switch(s)
{
case 1:
currnt.setDate();
break;

case 2:
currnt.getYearDifference();
break;

case -1:
break;

}
}while (s != -1);

return 0;
}

```

I get error: no matching function for call to `Date::getYearDifference()'
?_?

### #8 gabehabe

• GabehabeSwamp

Reputation: 1398
• Posts: 10,965
• Joined: 06-February 08

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 04:54 PM

Quote

no matching function for call to `Date::getYearDifference()'

You have a function by the same name, but the function that you have defined has to accept a parameter: int getYearDifference(Date & dOther)

### #9 Izumi

Reputation: 0
• Posts: 83
• Joined: 13-March 08

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 04:56 PM

gabehabe, on 25 May, 2008 - 04:54 PM, said:

Quote

no matching function for call to `Date::getYearDifference()'

You have a function by the same name, but the function that you have defined has to accept a parameter: int getYearDifference(Date & dOther)

And how would this happen?

### #10 gabehabe

• GabehabeSwamp

Reputation: 1398
• Posts: 10,965
• Joined: 06-February 08

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 05:13 PM

OK, I had a quick play with your code and came up with this:
```#include <iostream>
#include <Cstring>

using namespace std;

class Date
{
private:
int day,month,year;//the current date.
public:

int d_birth,m_birth,y_birth;//date of birth..
bool validDate(int d, int m, int y)
{
if (d < 1 || d > 31) return 0;
if (m < 1 || m > 12) return 0;
if (y < 1900) return 0;

return 1;
}
Date(int d,int m,int y)
{if (validDate(d, m, y))
{day=d;
month=m;
year=y;//the current day..
}
else cout<<"ERROR!! No such date. PLEASE Re-Start PROGRAM!";}

void setDate()
{
cout<<"enter ur birth day:";
cin>>d_birth;

if (d_birth<0 || d_birth>31)
cout<<"enter ur birth day agin please:";
else cout<<"enter ur birth month:";
cin>>m_birth;

if (m_birth<0 || d_birth>12)
cout<<"enter ur birth month agin please:";
else cout<<"enter ur birth year:";
cin>>y_birth;

}

int getYearDifference(Date & dOther) {
int years = dOther.year - this->year;
if (this->month > dOther.month || (this->month == dOther.month && this->day >= dOther.day) ) {
years--; // sorry baavgai, but it should be decrement, not increment
}
return years;
}

};

int main()
{ int x,y,z;
cin>>x>>y>>z; // get the birthday

Date bday(x,y,z);
cout << "Enter the current date: (dd/mm/yy)";
cin >>x>>y>>z; // get the current date
Date today(x,y,z);
int s;
do
{
cout<<"2-Calculate age"<<endl;
cout<<"choose One of this menu or '-1' to exit:"<<endl;
cin>>s;

switch(s)
{
case 1:
bday.setDate();
break;

case 2:
//output the difference (output the return value)
cout << "Your age: " << bday.getYearDifference(today); // pass today to the bday getYearDifference
break;

case -1:
break;

}
}while (s != -1);

return 0;
}
```

I think it's a little off of what you're aiming for, but it's there.

@baavgai:
Shouldn't years++ actually be years--?
It kept adding 2 years to my age, so I changed it around and it works now.

Hope this helps

### #11 Izumi

Reputation: 0
• Posts: 83
• Joined: 13-March 08

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 05:19 PM

It does help, but as you have added the Enter your birthday and current date above the menu, it pretty much makes the menu pointless. How should I get rid of the menu as all I use from it is case2.

### #12 Izumi

Reputation: 0
• Posts: 83
• Joined: 13-March 08

## Re: Calculating age with a class Date[defining function problem]

Posted 25 May 2008 - 11:01 PM

Oh, I came up with one more question - what if I want to calculate time from the date the user write in + a few years? as in a future date? How should the function int getYearDifference(Date & dOther) be changed so that it will be a new function for example int getFutureDate?

### #13 gabehabe

• GabehabeSwamp

Reputation: 1398
• Posts: 10,965
• Joined: 06-February 08

## Re: Calculating age with a class Date[defining function problem]

Posted 26 May 2008 - 04:14 AM

You wouldn't need to write a new function. Basically, your main should look something like this:
• Get today's date (this will be used no matter what)
• Print a menu - (1 is birthday, 2 is future date)
• switch the choice to decide which to do
• case 1: get the birthday and have this line:bday.getYearDifference (today);
• case 2: get the future date and have this line:today.getYearDifference (future);
Hope this helps

### #14 Izumi

Reputation: 0
• Posts: 83
• Joined: 13-March 08

## Re: Calculating age with a class Date[defining function problem]

Posted 27 May 2008 - 03:05 AM

Oaky....

This post has been edited by Izumi: 27 May 2008 - 03:05 AM

### #15 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

## Re: Calculating age with a class Date[defining function problem]

Posted 27 May 2008 - 08:54 AM

I had to do this in C#. I thought about it for a while, for whatever date they enter, you need to figure out if it's a leap year...and if the current year is a leap year. If the year is the same, how many days for each month, add up the months, but if a leap year the days change and if...if, if, if, seems like a thousand if's. The code would be a mile long! I told my instructor I couldn't do it.

Turns out, you don't have to do all that. In C#, there is a VERY simple way of doing this, and I would be surprised if there isn't one in C/C++ too.

Here's part of my code(that works) to give you a hint of what to look for in C/C++ (google). Remember this is C# so it may be similar but is probably not the exact same...but I don't know because I never tried it in C or C++.

```        private void button1_Click(object sender, EventArgs e)
{

DateTime x;
DateTime y;

//validate input
if (textBox1.Text == "" || textBox2.Text == "")
{ MessageBox.Show("Please enter a valid date in textboxes 1 and 2."); }

```

I hope this helps you!

edit--We had to figure the years, days, months and seconds, between any two dates the user enters. You may not need to worry about leap years if you only need to know the age in years.

This post has been edited by OliveOyl3471: 27 May 2008 - 08:59 AM