Nested Switch

I have a problem, the code runs but when it comes to the part when you choose what type of phone. it just ends there. Help is greatly appreciated. Thanks!


Edit: fixed the things Ganado pointed out and it worked 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  #include <iostream>
using namespace std;
int main()
{
	int brand;
	char type, model;
	cout<<"Brand: [1] Nokia [2] Sony Ericsson [3] Motorola [4] Samsung"<<endl;
	cout<<"Enter your choice: ";
	cin>>brand;
	switch(brand)
	{
	case 1:
	cout<<"Type: [A] Camera [B] Radio/MP3 [C] 3G"<<endl;
	cout<<"Enter your choice: ";
	cin>>type;
		switch(type)
		{
		case 'A': case 'a':
			cout<<"Model: [a] N77 [b] N93 [c] N95"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Nokia N77 Camera Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Nokia N93 Camera Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Nokia N95 Camera Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
			
		case 'B': case 'b':
			cout<<"Model: [a] N77 [b] N93 [c] N95"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Nokia N77 Radio/MP3 Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Nokia N93 Radio/MP3 Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Nokia N95 Radio/MP3 Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		case 'C': case 'c':
			cout<<"Model: [a] N77 [b] N93 [c] N95"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Nokia N77 3G Phone."<<endl;
				break;
			case 'b': case'B':
				cout<<"You selected the Nokia N93 3G Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Nokia N95 3G Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		default: 
			cout<<"Invalid Input."<<endl;
		}
		break;
	case 2:
		cout<<"Type: [A] Camera [B] Radio/MP3 [C] 3G"<<endl;
	cout<<"Enter your choice: ";
	cin>>type;
	
		switch(type)
		{
		case 'A': case 'a':
			cout<<"Model: [a] W8 [b] W995 [c] W205"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Sony Ericsson W8 Camera Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Sony Ericsson W995 Camera Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Sony Erricson W205 Camera Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		case 'B': case 'b':
			cout<<"Model: [a] W8 [b] W995 [c] W205"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Sony Ericsson W8 Radio/MP3 Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Sony Ericsson W995 Radio/MP3 Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Sony Ericsson W205 Radio/MP3 Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		case 'C': case 'c':
			cout<<"Model: [a] W8 [b] W995 [c] W205"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Sony Ericsson 3G Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Sony Ericsson 3G Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Sony Ericcson 3G Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		default: 
			cout<<"Invalid Input."<<endl;
		}
		break;
	case 3:
	cout<<"Type: [A] Camera [B] Radio/MP3 [C] 3G"<<endl;
	cout<<"Enter your choice: ";
	cin>>type;
	
		switch(type)
		{
		case 'A': case 'a':
			cout<<"Model: [a] Moto G [b] Moto X [c] Moto E"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Moto G Camera Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Moto X Camera Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Moto E Camera Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		case 'B': case 'b':
			cout<<"Model: [a] Moto G [b] Moto X [c] Moto E"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Moto G Radio/MP3 Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Moto X Radio/MP3 Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Moto E Radio/MP3 Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		case 'C': case 'c':
			cout<<"Model: [a] Moto G [b] Moto X [c] Moto E"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Moto G 3G Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Moto X 3G Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Moto E 3G Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		default: 
			cout<<"Invalid Input."<<endl;
		}
		break;
	case 4:
	cout<<"Type: [A] Camera [B] Radio/MP3 [C] 3G"<<endl;
	cout<<"Enter your choice: ";
	cin>>type;
	
		switch(type)
		{
		case 'A': case 'a':
			cout<<"Model: [a] Galaxy S [b] Galaxy Y [c] Galaxy Ace"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Galaxy S Camera Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Galaxy Y Camera Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Galaxy Ace Camera Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		case 'B': case 'b':
			cout<<"Model: [a] Galaxy S [b] Galaxy Y [c] Galaxy Ace"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Galaxy S Radio/MP3 Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Galaxy Y Radio/MP3 Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Galaxy Ace Radio/MP3 Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;
		case 'C': case 'c':
			cout<<"Model: [a] Galaxy S [b] Galaxy Y [c] Galaxy Ace"<<endl;
			cout<<"Enter your choice: ";
			cin>>model;
			
			switch(model)
			{
			case 'a': case 'A':
				cout<<"You selected the Galaxy S 3G Phone."<<endl;
				break;
			case 'b': case 'B':
				cout<<"You selected the Galaxy Y 3G Phone."<<endl;
				break;
			case 'c': case 'C':
				cout<<"You selected the Galaxy Ace 3G Phone."<<endl;
				break;
			default: 
				cout<<"Invalid Input."<<endl;
			}
			break;

		default: 
			cout<<"Invalid Input."<<endl;

		}
		break;
	
		default:
			cout<<"Invalid Input."<<endl;
		}
return (0);
}

Last edited on
when you choose what type of phone. it just ends there.

And what are you expecting it to do?
When you select one of the phone models, your program falls through to line 280 and exits.
http://www.cplusplus.com/forum/beginner/1988/

Some general comments.

1) You have a lot of repeated code. When you see repeated code in your program, it's a good clue you need to move the repeated code into functions.

2) Switch statements nested three deep. Ugh. Your only saving grace here is that you have decent indentation. Generally when I need nested switch statements, I prefer to have each branch of the outer switch statement call a function dedicated to that branch. Doing that gives a meaningful name to the branch and avoids directly nesting swtch statements. Note that this ties into #1 above. If you move the repeated code into functions, you're going to reduce the depth and complexiety of your nested switch statements.


type is an int, you're asking for a char. cin fails. Didn't check the rest, but with repeated code like that, chances are something else might be messed up. Try having functions that can...(can't think of a better word) function... on more generic inputs. Careful where you put your break statements.
Last edited on
Thank you guys! I've fixed the code and it ran well! Really thanks to you both! Will keep your tips in mind. Thanks again.
Topic archived. No new replies allowed.