B
ben.carbery
Hi,
I have just written a simple program to get me started in C that
calculates the number of days since your birthdate.
One thing that confuses me about the program (even though it works) is
how global variables and function returns work...
For example, I have a global array "char datestring[80];" which is
defined in the function speakdate. speakdate just converts a set of
integers (date variables) to a string.
The main program then does:
"printf ("The current date is:
%s\n\n",speakdate(cdate,cmonth,cyear));"
And correctly prints the value of datestring.
What I don't get is why this works since I haven't explicitly told
speakdate to return datestring.
I figured by default it would return nothing at all.
Full code below. By the way any other tips on my code would be welcomed
and appreciated!
cheers,
Ben C
I have just written a simple program to get me started in C that
calculates the number of days since your birthdate.
One thing that confuses me about the program (even though it works) is
how global variables and function returns work...
For example, I have a global array "char datestring[80];" which is
defined in the function speakdate. speakdate just converts a set of
integers (date variables) to a string.
The main program then does:
"printf ("The current date is:
%s\n\n",speakdate(cdate,cmonth,cyear));"
And correctly prints the value of datestring.
What I don't get is why this works since I haven't explicitly told
speakdate to return datestring.
I figured by default it would return nothing at all.
Full code below. By the way any other tips on my code would be welcomed
and appreciated!
cheers,
Ben C
Code:
char datestring[80];
/* dim is 'days in month'*/
int dim;
speakdate(int d, int m, int y)
{
char stdate[5],stmonth[10],styear[5];
sprintf( stdate, "%d", d);
sprintf( styear, "%d", y);
switch (d) {
case 1 :
case 21 :
case 31 : strcat(stdate,"st ");
/*printf("The date is %s\n",stdate);*/
break;
case 2 :
case 22 : strcat(stdate,"nd ");
/*printf("The date is %s\n",stdate);*/
break;
case 3 :
case 23 : strcat(stdate,"rd ");
/*printf("The date is %s\n",stdate);*/
break;
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
case 10 :
case 11 :
case 12 :
case 13 :
case 14 :
case 15 :
case 16 :
case 17 :
case 18 :
case 19 :
case 24 :
case 25 :
case 26 :
case 27 :
case 28 :
case 29 :
case 30 : strcat(stdate,"th ");
/*printf("The date is %s\n",stdate);*/
break;
default : strcat(stdate,"Invalid ");
/*printf("The date is %s\n",stdate);*/
break;
}
switch (m) {
case 1 : strcpy(stmonth,"January");
/*printf("The month is %s\n",stmonth);*/
break;
case 2 : strcpy(stmonth,"February");
/*printf("The month is %s\n",stmonth);*/
break;
case 3 : strcpy(stmonth,"March");
/*printf("The month is %s\n",stmonth);*/
break;
case 4 : strcpy(stmonth,"April");
/*printf("The month is %s\n",stmonth);*/
break;
case 5 : strcpy(stmonth,"May");
/*printf("The month is %s\n",stmonth);*/
break;
case 6 : strcpy(stmonth,"June");
/*printf("The month is %s\n",stmonth);*/
break;
case 7 : strcpy(stmonth,"July");
/*printf("The month is %s\n",stmonth);*/
break;
case 8 : strcpy(stmonth,"Aug");
/*printf("The month is %s\n",stmonth);*/
break;
case 9 : strcpy(stmonth,"Sep");
/*printf("The month is %s\n",stmonth);*/
break;
case 10 : strcpy(stmonth,"Oct");
/*printf("The month is %s\n",stmonth);*/
break;
case 11 : strcpy(stmonth,"Nov");
/*printf("The month is %s\n",stmonth);*/
break;
case 12 : strcpy(stmonth,"Dec");
/*printf("The month is %s\n",stmonth);*/
break;
default : strcpy(stmonth,"Invalid");
/*printf("The month is %s\n",stmonth);*/
break;
} /* end of switch */
strcpy(datestring,stdate);
strcat(datestring,stmonth);
strcat(datestring," ");
strcat(datestring,styear);
strcat(datestring,".");
/*printf("%s",datestring);*/
}
int countdays(int bd, int bm, int by, int cd, int cm, int cy)
{
int month,year;
int daysalive = 0;
/* count the days in whole years */
for (year=by+1;year<cy;year=++year) {
if (year==2000) {
daysalive = daysalive + 365;
/*printf("year is %d, adding 365, daysalive counted so
far is %d\n",year,daysalive);*/
}
else {
if (fmod(year,4)==0) {
daysalive = (daysalive + 366);
/*printf("year is %d, adding 366, daysalive counted
so far is %d\n",year,daysalive);*/
}
else {
daysalive = (daysalive + 365);
/*printf("year is %d, adding 365, daysalive counted
so far is %d\n",year,daysalive);*/
}
}
}
/*printf("%d\n",daysalive);*/
/* days in whole months this year */
for (month=cm-1;month>0;--month) {
daysalive = (daysalive + (monthswitch(month,year)));
/*printf("adding %d, month is
%d\n",(monthswitch(month,year)),month);*/
}
/*printf("%d\n",daysalive);*/
/* days so far in the current month */
daysalive = (daysalive + cd);
/*printf("%d\n",daysalive);*/
/* days in whole months in birth year */
for (month=bm+1;month<13;++month) {
daysalive = (daysalive + (monthswitch(month,year)));
}
/*printf("%d\n",daysalive);*/
/* days in birth month */
daysalive = (daysalive + (monthswitch(bm,by)) - bd);
/*printf("%d\n",daysalive);*/
return daysalive;
}
int monthswitch(int month,int year) {
switch (month) {
case 4 :
case 6 :
case 9 :
case 11 :
case 12 : dim = 30;
break;
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 : dim = 31;
break;
case 2 :
if (year==2000) {
dim = 28;
break;
}
if (fmod(year,4)==0) {
dim = 29;
break;
}
else
dim = 28;
break;
}
return dim;
}
main(void)
{
int bdate,bmonth,byear;
printf ("Please enter your birth date (d/m/yyyy):\n");
scanf ("%d/%d/%d",&bdate,&bmonth,&byear);
printf ("Your date of birth is
%s\n\n",speakdate(bdate,bmonth,byear));
int cdate,cmonth,cyear;
printf ("Please enter the current date (d/m/yyyy):\n");
scanf ("%d/%d/%d",&cdate,&cmonth,&cyear);
printf ("The current date is:
%s\n\n",speakdate(cdate,cmonth,cyear));
printf ("Congratulations! You were born a total of %d days
ago.\n",countdays(bdate,bmonth,byear,cdate,cmonth,cyear));
}