Well wrote the code but still I have some problems, so could someone help me plz.

``````void ageCalculator()
{
char birth[BIRTH],output[OUT];
static int finished,d[2],m[2],y[4],days,flag;

int i;
/*initialization of arrays*/
for(i=0;i<11;i++)
{
birth='\0';
if(i<6)
output='\0';
}

printf("\n Age Calculator\n");
printf("-------------------\n");
/* code to get and validate the input*/
do {
flag=FALSE;

do {
finished=0;
printf("\nEnter your date of birth (dd/mm/yyyy format): ");
fgets(birth, BIRTH+ 2, stdin);

if (birth[strlen(birth) - 1] != '\n')
{
printf("Input was too long.\n");
}
else
{
birth[strlen(birth) - 1] = '\0';
if(strlen(birth)==0)
return ;
finished = TRUE;
}

} while (!finished);

/*flag=valid(birth,d,m,y,0);*/
/*flag = TRUE;
if (!flag)
{
printf("\n invalid input");
printf("\nwhere.....");
d[0]=0;
m[0]=0;
y[0]=0;
}*/

}while(flag==FALSE);

/* code to get and validate the input*/
do {
flag=FALSE;
do {
finished=FALSE;
printf("\nEnter your date of birth (dd/mm/yyyy format): ");
fgets(birth, BIRTH+ 2, stdin);
if (birth[strlen(birth) - 1] != '\n') {
printf("Input was too long.\n");
}
else{
birth[strlen(birth) - 1] = '\0';
if(strlen(birth)==0)
return;
finished = TRUE;
}
} while (!finished);
/*flag=valid(birth,d,m,y,1);
*/if (!flag)
{
printf("\n invalid input ");
d[0]=0;
m[0]=0;
y[0]=0;
}
}while(flag==0);

if(y[0]==y[1])
{
days=countm(d,m,y);
}
else
{
days=county(d,m,y);
}
printf(output,"%d",days);
strcpy(output,"");
printf("\n%s\n\n");

}
int county(int d[],int m[],int y[])
{
int i,days=0,tempd[2],tempm[2],temp[2];

for(i=y[0]+1;i<y[1];i++)
{
days+=365;
temp[0]=i;
if(isleapyear(temp))
days+=1;
}

tempd[0]=d[0];
tempd[1]=31;
tempm[0]=m[0];
tempm[1]=12;
days+=countm(tempd,tempm,y);

tempd[0]=1;
tempd[1]=d[1];
tempm[0]=1;
tempm[1]=m[1];
y[0]=y[1];
days++;
days+=countm(tempd,tempm,y);

return days;
}
/*calculating the number of days if months are different*/
int countm(int d[],int m[],int y[])
{
int cal[12]={31,28,31,30,31,30,31,31,30,31,30,31},days,i;

if(m[0]==m[1])
{
days=d[1]-d[0];
}
else if(m[0]!=m[1]) {
days=0;
for(i=m[0]+1;i<m[1];i++)
days+=cal[i-1];
days=days+(cal[m[0]-1]-d[0])+d[1];

if((m[0]<=2&&m[1]>2)&& isleapyear(y))
days++;

}
return days;
}

/*code for calculating whether the given year is leapyear or not*/
int isleapyear(int y[])
{
if((y[0]%4==0)&&((y[0]%100!=0)||(y[0]%400==0)))
return 1;
else
return 0;
}
``````

Edited by happygeek: fixed formatting

4
Contributors
7
Replies
8
Views
12 Years
Discussion Span
Last Post by Dave Sinkula

code looks horrible. If you seriously want help then you should tag it with code tags to make it more readable and friendly to look thru.

Hi,
Well I couldn't make the file readable so I attached it, I hope you can help me plz.

Attachments

ok we are halfway there. Now maybe you could enlighten me as to the specific problems you are having.

Hi mna,
Well my problem is that when u run it it ask you to enter ur date of birth and when you entered its keep asking you again I don't know why, it should give the user how many days old is, could you please help me I never forget this please.):

For starters I would rework your validation code. You can use strchr and atoi to walk the dd/mm/yyyy string and pull out the ints or even sscanf. Make your validation code simpler and you may find your problem goes away.

can any one help me with a program in C to calculate the age from the given date of birth. Reply to << moderator edit: email snipped >>

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.