Julian date UDF

This is a program to print the Julian day of a particular date.That is the no. of days that have lapsed in the year therefar. The syntax is right but he logic is not... so..some help? Also..I want some tips on how to make the code look cleaner.

Thanks :-)







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include<iostream.h>

int julian(int d, int m, int y);
int main()

{
int d,m,y;
cout<<"enter date(dd)";
cin>>d;
cout<<"enter month(mm)";
cin>>m;
cout<<"Enter year(yyyy)";
cin>>y;


int f=julian(d,m,y);
cout<<"Julian day="<<f;
}

int julian(int d, int m, int y)

{int t=0,leap=0;
for(int i=1;i<m;i++){
switch(i){                      //adding days according to no. of months lapsed  

case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
        t+=31;
	break;
case 4:
case 6:
case 9:
case 11:
	t+=30;
	break;

case 2:                                     //also checking for leap yr
	t+=28;
	if((y%4==0)&&(y%100!=0) ||(y%400==0))
		leap++;
	break;
	

}
t+=d;

if(t>59&&leap==1)
t++;

}
return t;

}
Last edited on
You are lost because your indentation is horrible. That closing brace on line 56 should be before line 51.

Here it is with some better, and more consistent, indentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;

int julian(int d, int m, int y);

int main()
{
  int d,m,y;
  cout<<"enter date(dd)";
  cin>>d;
  cout<<"enter month(mm)";
  cin>>m;
  cout<<"Enter year(yyyy)";
  cin>>y;


  int f=julian(d,m,y);
  cout<<"Julian day="<<f;

  return 0;
}

int julian(int d, int m, int y)
{
  int t=0,leap=0;
  for(int i=1;i<m;i++)
  {
    switch(i)
    {                      //adding days according to no. of months lapsed  
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        t+=31;
	break;

      case 4:
      case 6:
      case 9:
      case 11:
	t+=30;
	break;

      case 2:                                     //also checking for leap yr
	t+=28;
	if (((y%4==0)&&(y%100!=0)) ||(y%400==0))
		leap++;
	break;
    }
  }

  t+=d;

  if(t>59&&leap==1)
    t++;

  return t;
}

Notice also the use of
1
2
#include <iostream>
using namespace std;
instead of the deprecated <iostream.h>.


BTW, the leap variable and the condition on line 53 are not necessary. If you are counting the whole month and it is a leap year, just add 29 to t instead of 28 (or just put t++ on line 46.

Hope this helps.
I'll take care of my indentation . And, I also see your point in the avoidability of initiating the leap variable. Thanks. But as I mentioned my major problem is in the logic of the program which I request you guys to analyse.
Huh? What exactly do you mean by 'analyse the logic of the program'? That stuff about where to move the closing for loop brace and the (lack of) need for the leap variable wasn't enough for you?
Oh. sorry, I din't notice the placechange of the brace. Thanks a lot.
Glad to have helped. :-)
Topic archived. No new replies allowed.