how do i put a loop around a switch statement?

i have the following switch statement to provide a menu. I want the menu to appear again after each selection. I tried using a "do - while" loop but it didn't work. :(

switch ( choice )

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


case 2:
compute(hotel_type res[], int size);
break;


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


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


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


default:
printf ("invalid Choice");
break;
}
Last edited on
1
2
3
4
do
{
  // .. the stuff you want to loop
}while( /*you want to keep looping*/ );

Hi caffrea4
As Disch said, do while loop must work provided you mention the exit condition otherwise it will go into the infinite loop.


I tried that. When i put in my choice the cursor just moves to the next line and the menu doesn't appear again. It doesn't let me enter anything i have to end the program.. what's wrong
Did you put your menu code in the loop?

If you want it to repeat (loop), then it belongs in the loop.
Yeah this is how i put it in :

do
{

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, 100);
break;


case 4:
print_res (res, 100);
break;


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


default:
printf ("invalid Choice");
break;
}
} while (choice <5);

Why is it just stopping when i enter a choice. Thanks for the replies by the way
compute(hotel_type res[], int size);
Wrong code. I think you want
1
2
3
4
5
6

hotel_type res[100];// declare res
int size=100;  
// ...

compute(res,  size);


Btw, use [code] [/code] tags

EDIT: too late
EDIT_2: maybe there's something wrong with one of the functions. Also try to enter an invalid choice - if program quits then do-while loop is OK.
Last edited on


I modified it to what you said but i get errors in compile :

Error E2108 program.c 68: Improper use of typedef 'hotel_type' in function main
Error E2121 program.c 68: Function call missing ) in function main
*** 2 errors in Compile ***
yeah when I put in an invalid choice the program ends, which is what i want so the loop works but my program doesn't??
Can you post more code?
ok here is the entire code, I'm useless

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
220
221
222
223
#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;

do
{

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, 100);
 		break;


		case 4:
 			print_res (res, 100);
 		break;


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


		default:
  			printf ("invalid Choice");
  		break;
	}
} while (choice <5);
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);

}
line 104... norecs has no initial value
Topic archived. No new replies allowed.