D
Daniel Rudy
When I try to compile the program below, I get these errors:
strata:/home/dcrudy/c/exercise 1131 $$$ ->cc -g -oex7-2 ex7-2.c
ex7-2.c: In function `conv_julian':
ex7-2.c:102: syntax error before `MONTHLENGTH'
ex7-2.c: At top level:
ex7-2.c:103: syntax error before `MONTHLEAPLENGTH'
ex7-2.c:110: `year' undeclared here (not in a function)
ex7-2.c:110: initializer element is not constant
ex7-2.c:110: warning: data definition has no type or storage class
ex7-2.c:111: warning: data definition has no type or storage class
ex7-2.c:112: `day' undeclared here (not in a function)
ex7-2.c:112: warning: data definition has no type or storage class
ex7-2.c:116: syntax error before `while'
strata:/home/dcrudy/c/exercise 1132 $$$ ->
For the life of me, I cannot seem to figure out why some of these are
errors. The compiler that I'm using is gcc 2.95.3. The ones that are
really puzzling is the errors on 110 and 112. It says that 'year' is
not in a function. It most definatly *IS* in a function, and it is
being used in that function. It also says these errors are at the top
level. What the hell? I followed the example in the book, so I'm at a
loss as to what the problem is.
Here's the program:
strata:/home/dcrudy/c/exercise 1135 $$$ ->cat -n ex7-2.c
1 /*
2
3 Programming Exercise 7-2 Page 111
4
5 Write a program to perform date arithmetic such as how many
6 days there are between 6/6/90 and 4/3/92.
7
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12
13
14 const int YEAR=365; /* Days in a year */
15 int LEAP=366; /* Days in a leap year */
16
17
18 char line[100]; /* user data input line */
19 int sday; /* day start input */
20 int smonth; /* month start input */
21 int syear; /* year start input */
22 int eday; /* day end input */
23 int emonth; /* month end input */
24 int eyear; /* year end input */
25 int jstart; /* julian start date */
26 int jend; /* julian end data */
27 int result; /* calculation result */
28
29
30 /* Checks to see if the given year is a leap year */
31 int check_leap(int year)
32 {
33 /* We need to check on 3 cases for this.
34 1. Every year divisible by 4 is a leap year.
35 2. But, if the year is divisible by 100, then it
36 is not a leap year.
37 3. Unless the year is also divisible by 400, then
38 it is still a leap year.
39 */
40
41 const int LEAPYEAR = 4; /* leap year modulus */
42 const int LEAPNONE = 100; /* leap 1 century modulus */
43 const int LEAPCENT = 400; /* leap 4 century modulus */
44 int result1;
45 int result2;
46 int result3;
47
48 /* Check if the year is > 399. Used for the every 400
49 years rule. */
50 if (year > 399)
51 {
52 result1 = year % LEAPYEAR;
53 result2 = year % LEAPNONE;
54 result3 = year % LEAPCENT;
55 }
56 else if (year > 99)
57 {
58 result1 = year % LEAPYEAR;
59 result2 = year % LEAPNONE;
60 result3 = 1;
61 }
62 else
63 {
64 result1 = year % LEAPYEAR;
65 result2 = 1;
66 result3 = 1;
67 }
68
69 /* Check if the given year is divisible by 4. If so, then
70 we need to check if the year is divisible by 100. If so,
71 then we also need to check if the year is divisible by 400.
72 If so, then it is a leap year. If not, then we do not have
73 a leap year. */
74 if (result1 == 0)
75 {
76 if (result2 == 0)
77 {
78 if (result3 == 0)
79 {
80 return(1);
81 }
82 else
83 {
84 return(0);
85 }
86 }
87 else
88 {
89 return(1);
90 }
91 }
92 else
93 {
94 return(0);
95 }
96 }
97
98
99 /* Takes the given date and converts it to the julian format */
100 int conv_julian(int month, int day, int year)
101 {
102 const int array MONTHLENGTH[12] =
{31,28,31,30,31,30,31,31,30,31,30,31};
103 const int array MONTHLEAPLENGTH[12] =
{31,29,31,30,31,30,31,31,30,31,30,31};
104
105 int leapflag; /* set to true if the given date is a
leap year. */
106 int count; /* counter for the month */
107 int daycount; /* day counter */
108
109 /* Init our variables */
110 leapflag = check_leap(year);
111 count = 0;
112 daycount = day;
113
114 /* Loop to add up the days of the months, minus
115 the current month. */
116 while (count < month - 1)
117 {
118 if (leapflag)
119 {
120 daycont = daycount + MONTHLEAPLENGTH[count];
121 }
122 else
123 {
124 daycont = daycount + MONTHLENGTH[count];
125 }
126 ++count;
127 }
128
129 /* Return our result */
130 return(daycount);
131 }
132
133
134
135 int main()
136 {
137 /* Get User Input */
138 printf("Input begining and ending dates (month day year) ");
139 fgets(line, sizeof(line), stdin);
140 sscanf(line, "%d %d %d %d %d %d", &smonth, &sday, &syear,
141 &emonth, &eday, &eyear);
142
143 /* convert given dates to julian format */
144 jstart = conv_julian(smonth, sday, syear);
145 jend = conv_julain(emonth, eday, eyear);
146
147 /* We have a couple of special cases to deal with */
148
149 /* Check for same year */
150 if (syear == eyear)
151 {
152 result = jend - jstart;
153 }
154 else
155 {
156 int leap; /* flag for if the begining year is a
leap */
157
158 leap = check_leap(syear);
159 if (leap)
160 {
161 result = (LEAP - jstart) + jend;
162 }
163 else
164 {
165 result = (YEAR - jstart) + jend;
166 }
167 if (syear + 1 > eyear)
168 {
169 int count; /* generic counter */
170
171 count = syear + 1;
172 while (count < eyear)
173 {
174 leap = check_leap(count);
175 if (leap)
176 {
177 result += LEAP;
178 }
179 else
180 {
181 result += YEAR;
182 }
183 ++count;
184 }
185
186 }
187 }
188
189 /* printout result */
190 printf("Difference in days is %d.\n", result);
191
192 /* return to operating system */
193 return(0);
194 }
195
196
strata:/home/dcrudy/c/exercise 1136 $$$ ->
strata:/home/dcrudy/c/exercise 1131 $$$ ->cc -g -oex7-2 ex7-2.c
ex7-2.c: In function `conv_julian':
ex7-2.c:102: syntax error before `MONTHLENGTH'
ex7-2.c: At top level:
ex7-2.c:103: syntax error before `MONTHLEAPLENGTH'
ex7-2.c:110: `year' undeclared here (not in a function)
ex7-2.c:110: initializer element is not constant
ex7-2.c:110: warning: data definition has no type or storage class
ex7-2.c:111: warning: data definition has no type or storage class
ex7-2.c:112: `day' undeclared here (not in a function)
ex7-2.c:112: warning: data definition has no type or storage class
ex7-2.c:116: syntax error before `while'
strata:/home/dcrudy/c/exercise 1132 $$$ ->
For the life of me, I cannot seem to figure out why some of these are
errors. The compiler that I'm using is gcc 2.95.3. The ones that are
really puzzling is the errors on 110 and 112. It says that 'year' is
not in a function. It most definatly *IS* in a function, and it is
being used in that function. It also says these errors are at the top
level. What the hell? I followed the example in the book, so I'm at a
loss as to what the problem is.
Here's the program:
strata:/home/dcrudy/c/exercise 1135 $$$ ->cat -n ex7-2.c
1 /*
2
3 Programming Exercise 7-2 Page 111
4
5 Write a program to perform date arithmetic such as how many
6 days there are between 6/6/90 and 4/3/92.
7
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12
13
14 const int YEAR=365; /* Days in a year */
15 int LEAP=366; /* Days in a leap year */
16
17
18 char line[100]; /* user data input line */
19 int sday; /* day start input */
20 int smonth; /* month start input */
21 int syear; /* year start input */
22 int eday; /* day end input */
23 int emonth; /* month end input */
24 int eyear; /* year end input */
25 int jstart; /* julian start date */
26 int jend; /* julian end data */
27 int result; /* calculation result */
28
29
30 /* Checks to see if the given year is a leap year */
31 int check_leap(int year)
32 {
33 /* We need to check on 3 cases for this.
34 1. Every year divisible by 4 is a leap year.
35 2. But, if the year is divisible by 100, then it
36 is not a leap year.
37 3. Unless the year is also divisible by 400, then
38 it is still a leap year.
39 */
40
41 const int LEAPYEAR = 4; /* leap year modulus */
42 const int LEAPNONE = 100; /* leap 1 century modulus */
43 const int LEAPCENT = 400; /* leap 4 century modulus */
44 int result1;
45 int result2;
46 int result3;
47
48 /* Check if the year is > 399. Used for the every 400
49 years rule. */
50 if (year > 399)
51 {
52 result1 = year % LEAPYEAR;
53 result2 = year % LEAPNONE;
54 result3 = year % LEAPCENT;
55 }
56 else if (year > 99)
57 {
58 result1 = year % LEAPYEAR;
59 result2 = year % LEAPNONE;
60 result3 = 1;
61 }
62 else
63 {
64 result1 = year % LEAPYEAR;
65 result2 = 1;
66 result3 = 1;
67 }
68
69 /* Check if the given year is divisible by 4. If so, then
70 we need to check if the year is divisible by 100. If so,
71 then we also need to check if the year is divisible by 400.
72 If so, then it is a leap year. If not, then we do not have
73 a leap year. */
74 if (result1 == 0)
75 {
76 if (result2 == 0)
77 {
78 if (result3 == 0)
79 {
80 return(1);
81 }
82 else
83 {
84 return(0);
85 }
86 }
87 else
88 {
89 return(1);
90 }
91 }
92 else
93 {
94 return(0);
95 }
96 }
97
98
99 /* Takes the given date and converts it to the julian format */
100 int conv_julian(int month, int day, int year)
101 {
102 const int array MONTHLENGTH[12] =
{31,28,31,30,31,30,31,31,30,31,30,31};
103 const int array MONTHLEAPLENGTH[12] =
{31,29,31,30,31,30,31,31,30,31,30,31};
104
105 int leapflag; /* set to true if the given date is a
leap year. */
106 int count; /* counter for the month */
107 int daycount; /* day counter */
108
109 /* Init our variables */
110 leapflag = check_leap(year);
111 count = 0;
112 daycount = day;
113
114 /* Loop to add up the days of the months, minus
115 the current month. */
116 while (count < month - 1)
117 {
118 if (leapflag)
119 {
120 daycont = daycount + MONTHLEAPLENGTH[count];
121 }
122 else
123 {
124 daycont = daycount + MONTHLENGTH[count];
125 }
126 ++count;
127 }
128
129 /* Return our result */
130 return(daycount);
131 }
132
133
134
135 int main()
136 {
137 /* Get User Input */
138 printf("Input begining and ending dates (month day year) ");
139 fgets(line, sizeof(line), stdin);
140 sscanf(line, "%d %d %d %d %d %d", &smonth, &sday, &syear,
141 &emonth, &eday, &eyear);
142
143 /* convert given dates to julian format */
144 jstart = conv_julian(smonth, sday, syear);
145 jend = conv_julain(emonth, eday, eyear);
146
147 /* We have a couple of special cases to deal with */
148
149 /* Check for same year */
150 if (syear == eyear)
151 {
152 result = jend - jstart;
153 }
154 else
155 {
156 int leap; /* flag for if the begining year is a
leap */
157
158 leap = check_leap(syear);
159 if (leap)
160 {
161 result = (LEAP - jstart) + jend;
162 }
163 else
164 {
165 result = (YEAR - jstart) + jend;
166 }
167 if (syear + 1 > eyear)
168 {
169 int count; /* generic counter */
170
171 count = syear + 1;
172 while (count < eyear)
173 {
174 leap = check_leap(count);
175 if (leap)
176 {
177 result += LEAP;
178 }
179 else
180 {
181 result += YEAR;
182 }
183 ++count;
184 }
185
186 }
187 }
188
189 /* printout result */
190 printf("Difference in days is %d.\n", result);
191
192 /* return to operating system */
193 return(0);
194 }
195
196
strata:/home/dcrudy/c/exercise 1136 $$$ ->