Programming Problem?

I have a repeat assignment due in a few days and can't get my program to compile. The assignment question is:


A hotel keeps records about all room reservations made on-line using a computer program. This program generated two files (named year2009.txt and year2010.txt) which store details on the reservations made in 2009 and 2010, respectively. Both files use the same format for storing the information, which is described next.
Each line contains 9 pieces of information about one reservation separated by space. The first two pieces of information represent the Surname and First_name of the person that made the reservation. The following three pieces of information are integer numbers that represent the check-in date in the format: Year_in Month_in Day_in (e.g. 2009 6 20 denotes the 20th of June 2009). The next three pieces of information are integers that represent the check-out date in the format of Year_out Month_out Day_out (e.g. 2010 6 25 denotes the 25th of June 2010). The last piece of information indicates the price of the room per day (Room_price).
You are required to develop a program which includes at least three functions (5 points), demonstrates good coding practices with regard to spacing, indentation, commenting, etc. (5 marks) and offers to the user a menu (5 points) for performing the following operations:
• Read all the information provided in both files: year2009.txt and year2010.txt (5 points) and store it in an array of structures (5 points). The records in the memory should also indicate the year. It is assumed that there are no more than 100 reservations made in one year.
• Compute the total number of booked days for each reservation made in 2009 and 2010 (5 points), and store it in the memory together with the other details about the reservation (5 points). The following formula should be used:
IF (Day_out > Day_in) THEN
Total_days = Day_out – Day_in
ELSE
Total_days = Day_out + (30 – Day_in)
Note: For simplification, it is assumed that on average a month has 30 days and no reservation can be made for more than 30 days.
Example: If a reservation was performed for the following period: Check-in date: 2009 6 20 (20 June 2009) and Check-out date: 2009 7 2, the total number of booked days is 12.
• Compute the total amount received by the hotel for each reservation made and store it in the memory together with the other details about the reservation (5 points). The following formula should be used for each reservation:
Total = Total_days * Room_price
• Print on the screen Surname, First Name and Total amount for those persons who paid over 1000 euro for a reservation made with the hotel in 2010 (5 points).
• Save in a file named week2009.txt all the reservations from 2009 that were made for at least 7 days. The following information will be saved for each reservation: Surname and First Name of the person as well as Total amount paid (5 points).



here is my attempt at the code:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdio.h>
#include <stdlib.h>


typedef struct hotel_tag

{

char surname[20];
char first_name[20];
int year_in;
int year_out;
int month_in;
int month_out;
int day_in;
int day_out;
int num_days;
float price;
float tot_price;

} hotel_type;



int read_info(char* file_name, hotel_type res[]);

void compute(hotel_type res[], int size);

void compute_total(hotel_type res[], int size);

void print_res (hotel_type res[], int size);

void write_records (char* file_name, hotel_type res[], int size);


int main()

{
 
hotel_type res[100];
int norecs;
int choice;

printf ("welcome to my program\n\n");
printf ("MENU\n\n");
printf ("1.\tRead data from files\n");
printf ("2.\tcompute total number of booked days for 2009 and 2010\n");
printf ("3.\tCompute total amount received by hotel\n");
printf ("4.\tShow on screen the details of persons who paid over 1000 euro\n");
printf ("5.\tSave data to file: week2009.txt\n\n");
printf ("Please enter your choice.\n\n");

scanf ("%d",choice);

switch ( choice ) 

	{
		case 1:
  			norecs = read_info("year2009.txt", res);
 		break;
			

		case 2:
 			compute (res[], 100);
 		break;


		case 3:
 			compute_total(res[], int size);
 		break;


		case 4:
 			print_res (res[], int size);
 		break;


		case 5:
 			write_records ("week2009.txt", res, norecs);
 		break;


		default:
  			printf ("invalid Choice");
  		break;
	}

return (EXIT_SUCCESS);

}


int read_info (char* file_name, hotel_type res[])

{

FILE* fp;
int norecs;
char ch;

fp = fopen(file_name, "r");

while (!feof(fp))
	
	{
	
	fscanf (fp, "%c%c%d%d%d%d%d%d%f", &res[norecs].surname, 
					  &res[norecs].first_name,
					  &res[norecs].year_in,
					  &res[norecs].month_in,
					  &res[norecs].day_in,
					  &res[norecs].year_out,
					  &res[norecs].month_out,
					  &res[norecs].day_out,
					  &res[norecs].price);

	norecs++;

	}
fclose(fp);

return norecs;

}







void compute(hotel_type res[], int size)

{

int i;

for (i=0; i < size; i++)

	if (res[i].day_out > res[i].day_in)

		res[i].num_days = res[i].day_out - res[i].day_in;

	else

		res[i].num_days =  res[i].day_out + (30 - res[i].day_in);

}






void compute_total(hotel_type res[], int size)

{

int i;

for (i=0; i < size; i++)

	res[i].tot_price = res[i].num_days * res[i].price;


}





void print_res (hotel_type res[], int size)

{

int i;

printf ("\t| Surname\t | First name\t | Total amount |\n\n");

	for(i=0; i < size; i ++)

		if (res[i].tot_price > 1000)

		printf ("\t| %s\t | %s\t | %f", res[i].surname, res[i].first_name, res[i].tot_price);

}





void write_records (char* file_name, hotel_type res[], int size)

{

FILE* fp;

int i;


fp = fopen(file_name, "w");


for (i = 0; i < size; i++)

fprintf (fp, "%s %s %f \n", res[i].surname, 
			    res[i].first_name,
			    res[i].tot_price);


fflush(fp);



fclose(fp);

}


i get the following errors in compile:

Error E2188 program.c 64: Expression syntax in function main
Error E2188 program.c 69: Expression syntax in function main
Error E2188 program.c 74: Expression syntax in function main
*** 3 errors in Compile ***


Any help at all would be great. Cheers!


Last edited on
Hi

I have not read your entire description but from the errors it seems that
solution to your problem is in the error lines itself.
you don't have to pass res[] inside the function. just pass res. It will automatically interpret it as a array incase you have declared it as an array.
thanks. I got it to compile with no errors but isn't working correctly.
it would be helpful if you mention exactly what problem you are facing now in sort.
oh sorry ha. When I run the program the menu appears but when i enter my choice it executes the code but then stops and doesn't let me enter anything. i have to keep ending the program.
I have metioned some comments .. Try them.. I have not tested your code..so it might have few more errors as well.
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdio.h>
#include <stdlib.h>


typedef struct hotel_tag

{

char surname[20];
char first_name[20];
int year_in;
int year_out;
int month_in;
int month_out;
int day_in;
int day_out;
int num_days;
float price;
float tot_price;

} hotel_type;



int read_info(char* file_name, hotel_type res[]);

void compute(hotel_type res[], int size);

void compute_total(hotel_type res[], int size);

void print_res (hotel_type res[], int size);

void write_records (char* file_name, hotel_type res[], int size);


int main()

{

hotel_type res[100];
int norecs;
int choice;

printf ("welcome to my program\n\n");
printf ("MENU\n\n");
printf ("1.\tRead data from files\n");
printf ("2.\tcompute total number of booked days for 2009 and 2010\n");
printf ("3.\tCompute total amount received by hotel\n");
printf ("4.\tShow on screen the details of persons who paid over 1000 euro\n");
printf ("5.\tSave data to file: week2009.txt\n\n");
printf ("Please enter your choice.\n\n");

scanf ("%d",choice);

switch ( choice ) 

{
case 1:
norecs = read_info("year2009.txt", res);
break;


case 2:
compute (res[], 100);	//remove []
break;


case 3:
compute_total(res[], int size); //remove [] & you can't pass int size. you have to pass the actual value.
break;


case 4:
print_res (res[], int size);	//remove [] & you can't pass int size. you have to pass the actual value.
break;


case 5:
write_records ("week2009.txt", res, norecs);
break;


default:
printf ("invalid Choice");
break;
}

return (EXIT_SUCCESS);

}


int read_info (char* file_name, hotel_type res[])

{

FILE* fp;
int norecs;
char ch;

fp = fopen(file_name, "r");

while (!feof(fp))

{

fscanf (fp, "%c%c%d%d%d%d%d%d%f", &res[norecs].surname, 
&res[norecs].first_name,
&res[norecs].year_in,
&res[norecs].month_in,
&res[norecs].day_in,
&res[norecs].year_out,
&res[norecs].month_out,
&res[norecs].day_out,
&res[norecs].price);		//you should initialize norecs before using it.

norecs++;

}
fclose(fp);

return norecs;

}







void compute(hotel_type res[], int size)

{

int i;

for (i=0; i < size; i++)

if (res[i].day_out > res[i].day_in)

res[i].num_days = res[i].day_out - res[i].day_in;

else

res[i].num_days = res[i].day_out + (30 - res[i].day_in);

}






void compute_total(hotel_type res[], int size)

{

int i;

for (i=0; i < size; i++)

res[i].tot_price = res[i].num_days * res[i].price;


}





void print_res (hotel_type res[], int size)

{

int i;

printf ("\t| Surname\t | First name\t | Total amount |\n\n");

for(i=0; i < size; i ++)

if (res[i].tot_price > 1000)

printf ("\t| %s\t | %s\t | %f", res[i].surname, res[i].first_name, res[i].tot_price);

}





void write_records (char* file_name, hotel_type res[], int size)

{

FILE* fp;

int i;


fp = fopen(file_name, "w");


for (i = 0; i < size; i++)

fprintf (fp, "%s %s %f \n", res[i].surname, 
res[i].first_name,
res[i].tot_price);


fflush(fp);



fclose(fp);

}


scanf ("%d",choice);

should be
scanf ("%d",&choice);
Topic archived. No new replies allowed.