# Thread: Calculating number of days for a given date code suggestions/how to func?

1. ## Calculating number of days for a given date code suggestions/how to func?

Code:
```#include <stdio.h>

int main()
{
int d,m,y,month=0;
longint t;
printf("enter a date dd-mm-yyyy: ");
scanf("%d-%d-%d", &d, &m, &y);
if (d<=0||d>31) {
printf("wrong date format!");
}else if(m<=0||m>12){
printf("wrong date format!");
}else if(y<0){
printf("Jesus was not born on that date that's all I know");
}

if(m>2 && y%4==0 && y%100!=0 || m>2 && y%400==0)
d=d+1;

(long int)t=(y-1)*365.25+month+d;
switch (m) {
case 1:month=d;
case 2:month=31+d;
case 3:month=59+d;
case 4:month=90+d;
case 5:month=120+d;
case 6:month=151+d;
case 7:month=181+d;
case 8:month=212+d;
case 9:month=243+d;
case 10:month=273+d;
case 11:month=304+d;
case 12:month=334+d;
default:break;

}printf("%li days past since jesus was born", t);
getchar();
return0;

}```
I wrote this code myself and I am pretty new in C programming I just like have suggestions about what I did wrong and right and I have some questions if u can help
1.I dont know intending format checked on internet couldnt find exact answer
2.I tried changed this a function compiler said nested functions are disabled
3.I did casting for "t" but I dont know what value I will get for instance for x.75 will compiler give me x or x+1?

2. Indentation

2 -- Are the number of the closing brackets equal to the number of the closing ones??

3 -- run it to find out !

3. Originally Posted by std10093
2 -- Are the number of the closing brackets equal to the number of the closing ones??
I sure hope so

4. You are right Matticus.

5. (long int)t=(y-1)*365.25+month+d;
Try this:
t=(y-1)*365.25+month+d;

6. Code:
```(long int)t=(y-1)*365.25+month+d;
switch (m) {
case 1:month=d;
case 2:month=31+d;
...```
What value has "month" when you calculate "t"?

Bye, Andreas

7. also looks to me that the switch will add up EVERY month, instead of just the one you want....no breaks on each case?

8. Jesus was almost certainty not born on Dec 31 nor Dec 25, or in the year 0.

4-6 BC is more likely and probably in September.

9. Code:
```#include <stdio.h>

int main()
{
int d,m,y,month=0;
longint t;
printf("enter a date dd-mm-yyyy: ");
scanf("%d-%d-%d", &d, &m, &y);
if (d<=0||d>31) {
printf("wrong date format!");
}else if(m<=0||m>12){
printf("wrong date format!");
}else if(y<0){
printf("Jesus was not born on that date that's all I know");
}

if(m>2 && y%4==0 && y%100!=0 || m>2 && y%400==0)
d=d+1;

switch (m) {
case 1:month=0;
break;
case 2:month=31;
break;
case 3:month=59;
break;
case 4:month=90;
break;
case 5:month=120;
break;
case 6:month=151;
break;
case 7:month=181;
break;
case 8:month=212;
break;
case 9:month=243;
break;
case 10:month=273;
break;
case 11:month=304;
break;
case 12:month=334;
break;
default:break;}

t=((y-1)*365.25)+month+d;

printf("%li days past till %d-%d-%d, since date started ", t,d,m,y);
getchar();
return0;

}```
I have tried to fix the code and came out like this, check it pls, anymore suggestions ?
thanks to everybody

10. Originally Posted by std10093

2 -- Are the number of the closing brackets equal to the number of the closing ones??
I am sorry, but I didn't understand what you mean

11. Their are the same amount of closing brackets with opening brackets.

You should put fflush stdin before getchar();

Code:
```    fflush(stdin);
getchar();```

12. Originally Posted by 0x47617279
Their are the same amount of closing brackets with opening brackets.

You should put fflush stdin before getchar();

Code:
```    fflush(stdin);
getchar();```
No! Dont use "fflush(stdin)" - it results in undefined behavior.
FAQ > Why fflush(stdin) is wrong - Cprogramming.com