# calculate age

Page 1 of 1

## 5 Replies - 7818 Views - Last Post: 20 November 2008 - 02:13 PMRate 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=72718&amp;s=c8831722ed69b8a1382d77a0d4716905&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bert3ls

• New D.I.C Head

Reputation: 0
• Posts: 26
• Joined: 08-October 08

# calculate age

Posted 19 November 2008 - 11:46 PM

i have been working on this program.. i almost got everything going... however.. i need to calculate for the age...
how can i do that?
here is a copy of my program so u can see when i have done.
/*family
*
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* declare atio*/
#include <conio.h>

struct family
{
char name [50];	/* persons name*/
char street [50];  /* street address*/
char csz [50];	 /* city, state, zip */
char relation [30];/* relation to you */
char birthday [20];/* mm-dd-yy */
int age;
};
struct family people[5];
union birthday
{
char ddmmyy [10];
char mmmddyyyy [25];
};

void fillmember (int index, char aname[], char astreet[], char acsz[], char arelation[], char abirthday[], int anage)
{
strcpy (people[index].name, aname);
strcpy (people[index].street, astreet);
strcpy (people[index].csz, acsz);
strcpy (people[index].relation, arelation);
strcpy (people[index].birthday, abirthday);
people[index].age= anage;
}

void main (void)
{
FILE *in, *out;
char astring[81];
char message[50]="These lines came from in.txt \n";
int i=0; /*index into array of numbers*/
int num;
int bday;

printf ("This program reads from in.txt and write to out.txt.\n\n");
in = fopen ("c:\\in.txt", "r"); /*openfiles*/
out = fopen ("c:\\out.txt", "w");

/*read in and print out	*/
fgets(astring,81,in);
printf("%s  ", astring);
num=atoi(astring);

if (in==NULL ||out==NULL)
{
printf("ERROR");
getch();
return;
}
else
{
fprintf (out,message); /*write to file*/
while( !feof(in))
{
for(i=0;i<=4;i++)
{

fscanf (in,"%s %s\n", &people[i].name);
printf ("name= %s %s\n", people[i].name);
fprintf (out,"%s\n", people[i].name );
printf ("The text from in.txt is\n%s %s\n\n", people[i].name);

fscanf (in,"%s %s \n",&people[i].street);
printf ("street= %s %s \n",people[i].street);
fprintf (out,"%s\n", people[i].street);
printf ("The text from in.txt is\n%s%s\n\n", people[i].street);

fscanf (in,"%s %s\n",&people[i].csz);
printf ("csz= %s %s\n",people[i].csz);
fprintf (out,"%s\n", people[i].csz);
printf ("The text from in.txt is\n%s\n\n", people[i].csz);

fscanf (in,"%s \n",&people[i].relation);
printf ("relation= %s \n",people[i].relation);
fprintf (out,"%s \n", people[i].relation);
printf ("The text from in.txt is\n%s\n\n", people[i].relation);

fscanf (in,"%s \n",&people[i].birthday);
printf ("birthday= %s \n", people[i].birthday );
fprintf (out,"%s \n", people[i].birthday);
printf ("The text from in.txt is\n%s\n\n", people[i].birthday);

fprintf (out,"\n %s%s %s\n", people[i].name, people[i].relation);
printf ("\n%s	   relation:%s", people[i].name, people[i].relation);
fprintf (out,"%s%s", people[i].street, people[i].birthday);
printf ("\n%s	   birthday:%s", people[i].street, people[i].birthday);
fprintf (out,"%s\n", people[i].csz );
printf ("\n%s	   age:%i  \n\n\n\n\n", people[i].csz, people[i].age);

}
}
}

printf ("\n");
fclose(out);
fclose(in);
}



** Edit **

Is This A Good Question/Topic? 0

## Replies To: calculate age

• Lux Ex Tenebris

Reputation: 14
• Posts: 540
• Joined: 30-October 08

## Re: calculate age

Posted 19 November 2008 - 11:57 PM

bert3ls, on 19 Nov, 2008 - 10:46 PM, said:

i have been working on this program.. i almost got everything going... however.. i need to calculate for the age...
how can i do that?
here is a copy of my program so u can see when i have done.

That's some pretty hefty code for a beginner, I'm impressed.

Does what you have done so far work? Does it compile and run?

So are you saying you only take in their birthday and from that you need to generate how old they are?

If that is so I see one possible problem. Where do you set todays date in the program? I might have missed it but if not, you can't figure out how old they are by birthdate if you don't run it against today's date.

If that's covered and I missed it than you should look here calculating age was covered in another D.I.C. discussion thread.

Let us know if this prior discussion doesn't cover what u need ok?

### #3 bert3ls

• New D.I.C Head

Reputation: 0
• Posts: 26
• Joined: 08-October 08

## Re: calculate age

Posted 20 November 2008 - 01:15 AM

thanks... iv been at it for several hours....
i see what u mean by setting today's date... i did not put that in.
what i have.. so far everything pretty much prints out correctly. I just need to change the birth date from
ex. 11/19/08
to
November, 19 2008
then I need to calculate the persons age...
i tried playing around with the source you linked me to, on my program, but I did not manage to make it work out..

badjava, on 19 Nov, 2008 - 10:57 PM, said:

bert3ls, on 19 Nov, 2008 - 10:46 PM, said:

i have been working on this program.. i almost got everything going... however.. i need to calculate for the age...
how can i do that?
here is a copy of my program so u can see when i have done.

That's some pretty hefty code for a beginner, I'm impressed.

Does what you have done so far work? Does it compile and run?

So are you saying you only take in their birthday and from that you need to generate how old they are?

If that is so I see one possible problem. Where do you set todays date in the program? I might have missed it but if not, you can't figure out how old they are by birthdate if you don't run it against today's date.

If that's covered and I missed it than you should look here calculating age was covered in another D.I.C. discussion thread.

Let us know if this prior discussion doesn't cover what u need ok?

### #4 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: calculate age

Posted 20 November 2008 - 01:54 AM

Were you looking for something like this ...

/* returns the name of the month number passed in ... in range 1..12 */
char * toName( int m )
{
if( m<1 || m> 12 ) return "No such month";

char * nameOfMonth[]=
{
"January", "February", "March",  "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
return nameOfMonth[ m-1 ];
}



A variant is used in the following program ...

/*
Dates ... with C

Some (a little and/or not so) novel 'dating' pointers ... for C students.

To strengthen your foundations in programming,
or to contact the author, please look here ...

[url="http://developers-heaven.net/forum/index.php/topic,46.0.html"]http://developers-heaven.net/forum/index.php/topic,46.0.html[/url]
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct date /* the main attraction */
{
int  mm,
dd,
yy;
}date;

/* 11 very forward declarations ...*/

date * getNewDate();
date * newCopy( date *d );
void show( date *d );
void advance( date *d );
int isValid( date *d );
int isLeapYear( int year );
int isLess( date *d1, date *d2 );
int isEqual( date *d1, date *d2 );
int exceedsDaysInMonth( date *d );
int difference( date d1, date d2 );
int more();

/* Note well the progression above, but the main objext, is 'to get a date'. */

int main() /* to test out all the 'dating tips' introduced above */
{
date *pd;
date *pd2;
int c, i, n;
do
{
pd = getNewDate();
printf("You entered the date : "); show( pd );
printf("\nTomorrow's date is   : "); show( pd );

for(i=1; i<=6; ++i) advance( pd );
printf("\nOne weeks date is    : "); show( pd );

for(i=1; i<=21; ++i) advance( pd );
printf("\nFour weeks date is   : "); show( pd );

for(i=1; i<=365-28; ++i) advance( pd );
printf("\n365 days date is     : "); show( pd );

printf("\n\nEnter a 'base' date to count from ...");
free( pd );

pd = getNewDate();
pd2 = newCopy( pd ); /*  make a 'new' backup copy ... */
printf("You chose the date   : "); show( pd );

printf("\nEnter the number of days to advance : ");
scanf("%d", &n);
while((c=getchar())!='\n') c=getchar(); /* 'flush' stdin ... */
for(i=1; i<=n; ++i) advance( pd );
printf("%d days into the future is : ", n); show( pd );

if( isLess( pd, pd2 ) ) printf("\nisLess( pd, pd2 ) is true.\n");
else printf("\nisLess( pd, pd2 ) is false.\n");

printf("\n\nDouble ckecking days between ...\n");
show( pd ); printf( " and " );  show( pd2 );
n =  difference( *pd, *pd2 );
printf("\nDays between equals  : %d", n);
printf("\nAs 'inclusive' days  : %d", 1+n);

printf("\n\nDouble ckecking days between ...\n");
show( pd2 ); printf( " and " );  show( pd );
n =  difference( *pd2, *pd );
printf("\nDays between equals  : %d", n);
printf("\nAs 'inclusive' days  : %d", 1+n);

free( pd2 );
free( pd );

}while( more() );

return 0;
}

/* 11 not so secret date function definition tips ...*/

int isLeapYear( int year )
{
if (year%4 != 0) return 0;  /* not a leap year */
if (year%100 == 0 && year%400 != 0) return 0; /* not a leap year */
return 1;
}

int exceedsDaysInMonth( date *d )
{
int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

if( !isLeapYear( d->yy ) )
{
if ( d->mm > 12 || d->dd > days[d->mm] ) return 1;

return 0;
}
else /* is leap year ... so need to handle case of month == Feb */
{
if ( d->mm == 2 && d->dd == 29 ) /* <-- the 'tricky' condition to get right */
return 0;  /* NOTE: returns FALSE here since date IS VALID */
if ( d->mm > 12 || d->dd > days[d->mm] )
return 1;

return 0;
}
}

void advance( date *d )
{
d->dd++;                       /* first increment the day */
if( exceedsDaysInMonth( d ) )  /* if it crosses over to the next month */
{
d->dd = 1;                  /* the day becomes the first of next month */
d->mm++;                    /* increment month in that case */
if(d->mm > 12)              /* if it crosses over to the next year */
{
d->mm = 1;              /* the month becomes the first month */
d->yy++;                /* of the next year */
}
}
}

int isValid( date *d )
{
/* if month not in 1..12 invalid ... */
if( d->mm< 1 || d->mm > 12 ) return 0;
if ( exceedsDaysInMonth( d ) ) return 0;
/*
The following bit of text was copied from an HLA program by Randy Hyde

The following is questionable.
Who knows what ranges we should check? 1600 is probably a good starting year
since this was shortly after the advent of the Gregorian calendar.  The upper
end should be reasonably small so as to catch ridiculous dates, but large
enough so that we don't suffer from the "Y2K Syndrome".  9999 is probably
way too big (2200 would probably be a good number for now), but who knows
what to use here?
*/
if (d->yy <1600 || d->yy>9999) return 0;
return 1;
}

int isLess( date *d1, date *d2 )
{
if( d1->yy < d2->yy ) return 1;
if( d1->yy <= d2->yy && d1->mm < d2->mm ) return 1;

if(
d1->yy <= d2->yy
&& d1->mm <= d2->mm
&& d1->dd < d2->dd
)
return 1;
/* if reach here ... */
return 0;
}

int isEqual( date *d1, date *d2 )
{
if (
d1->yy == d2->yy
&& d1->mm == d2->mm
&& d1->dd == d2->dd
)
return 1;
/* if reach here ... */
return 0;
}

date * newCopy( date *d )
{
date * tmp = (date*) malloc( sizeof(date) );
tmp->dd = d->dd;
tmp->mm = d->mm;
tmp->yy = d->yy;
return tmp;
}

int difference( date d1, date d2 )
{
int diffDays = 0;
if( isLess( &d1, &d2 ) )
{
while( !isEqual(&d1, &d2) )
{
diffDays++;
}
}
else
{
while( !isEqual(&d2, &d1) )
{
diffDays++;
}
}
return diffDays;
}

date * getNewDate()
{
date * d = (date*) malloc( sizeof(date) );
int c;
printf("\nEnter a valid date in the format yyyy/mm/dd : ");
for(;;)/> /* loop forever ... until break */
{
scanf("%d",&d->yy);
getchar(); /* to 'eat' the '/' char's ... */
scanf("%d",&d->mm);
getchar();
scanf("%d",&d->dd);
while((c=getchar())!='\n') c=getchar(); /* 'flush' stdin ... */

if( isValid(d) ) break;

/* if program reaches here ... */
printf
(
"\nInvalid date. "
"Enter date in range 1600/1/1..9999/12/31 : "
);
}
return d;
}

void show( date *d )
{
char * nameOfMonth[]=
{
"***", "January", "February", "March",  "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
printf("%s %d, %d", nameOfMonth[d->mm], d->dd, d->yy);
}

int more()
{
printf("\n\nMore (y/n) ? ");
int dummy = (int)reply;
while(dummy!='\n') dummy = getchar();
}



This post has been edited by David W: 20 November 2008 - 03:26 AM

### #5 bert3ls

• New D.I.C Head

Reputation: 0
• Posts: 26
• Joined: 08-October 08

## Re: calculate age

Posted 20 November 2008 - 01:52 PM

i fixed some errors i had in the program... now before i acutally get to calculating the age and modifying the birth date...
there is one more porblem i need to fix.
in the print out the information comes out like this:
Ex.
John Smith
Relation: Self
12 fake street
Birthday: 11-19-08
Middle of, NW 9999
Age: 0

It needs to come out
John Smith Relation:Self
12 fake street Birhtday: 11-19-08
Middle of, NW 9999 Age:0

how can i do that?
here is the modified code:

/*family
*
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* declare atio*/
#include <conio.h>

struct family
{
char name [50];	/* persons name*/
char street [50];  /* street address*/
char csz [50];	 /* city, state, zip */
char relation [30];/* relation to you */
char birthday [20];/* mm-dd-yy */
int age;
};
struct family people[5];
/*
union bday
{
char ddmmyy [10];
char mmmddyyyy [25];
};
*/

void main (void)
{
FILE *in, *out;
char lines[81];
char message[50]="These lines came from in.txt \n";
int i; /*index into array of numbers
int num;*/

printf ("This program reads from in.txt and write to out.txt.\n\n");
in = fopen ("c:\\in.txt", "r"); /*openfiles*/
out = fopen ("c:\\out.txt", "w");

if (in==NULL ||out==NULL)
{
printf("ERROR");
getch();
return;
}
else
{
fprintf (out,message); /*write to file*/
for(i=0;i<=4;i++)	   /*(use gets   not fscan) */
{

fgets (lines, 81, in);
strcpy (people[i].name, lines);
printf ("%s", people[i].name);

fgets (lines, 81, in);
strcpy (people[i].street, lines);
printf ("%s", people[i].street);

fgets (lines, 81, in);
strcpy (people[i].csz, lines);
printf ("%s", people[i].csz);

fgets (lines, 81, in);
strcpy (people[i].relation, lines);
printf ("%s", people[i].relation);

fgets (lines, 81, in);
strcpy (people[i].birthday, lines);
printf ("%s\n\n", people[i].birthday);

}

for(i=0;i<=4;i++)
{
fprintf (out,"\n %s relation:%s", people[i].name, people[i].relation);
printf ("\n\n%s	   relation:%s", people[i].name,people[i].relation);
fprintf (out,"\n%s	 birhtday:%s", people[i].street, people[i].birthday);
printf ("\n%s		birthday:%s", people[i].street, people[i].birthday);
fprintf (out,"\n %s", people[i].csz );
printf ("\n%s		age:%i  \n\n\n", people[i].csz, people[i].age);

}
}

printf ("\n");
fclose(out);
fclose(in);
}



This post has been edited by bert3ls: 20 November 2008 - 01:53 PM

• Lux Ex Tenebris

Reputation: 14
• Posts: 540
• Joined: 30-October 08

## Re: calculate age

Posted 20 November 2008 - 02:13 PM

bert3ls, on 20 Nov, 2008 - 12:52 PM, said:

i fixed some errors i had in the program... now before i acutally get to calculating the age and modifying the birth date...
there is one more porblem i need to fix.
in the print out the information comes out like this:
Ex.
John Smith
Relation: Self
12 fake street
Birthday: 11-19-08
Middle of, NW 9999
Age: 0

It needs to come out
John Smith Relation:Self
12 fake street Birhtday: 11-19-08
Middle of, NW 9999 Age:0

I haven't done this quite the way you are attempting to do but I think you can still just add the two strings together so name += relation and then just print name. Just make sure the field is big enough to hold the two variables together.

Also you might want to look up the built in string functions like strncat (and strcat). The rest of the string functions are pretty handy too, it's something good to start trying out.

Try this link out for string functions.