switch case not working

Hi all, i would like to find out why my nested switch case does not work. It just hangs when i key in 1 in case 4.
As you can see in case 4, i am doing a nested switch case. However, when i key in 1, the program just hangs. May i know what is the reason behind this?




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
#include <algorithm>
#include <sstream>
#include <fstream>
#include <functional>
#include <iostream>
#include <string>
#include "ShapeTwoD.h"
#include "Cross.h"
#include "Square.h"
#include "Rectangle.h"
using namespace std;


string arrayOfShapes[100];


struct my_function  // boolean function that was used in the sort function on line 175
{
    bool operator() ( const string& a, const string& b ) const
   {
        float convertA = 0, convertB = 0 ;
        std::stringstream(a) >> convertA ;
        std::stringstream(b) >> convertB ;
        return convertA  > convertB ;
   }
};


void showProgram()
{
      
      cout<<"Welcome to Assn2 program!"<<endl<<
   
      "1)Input sensor data."<<endl<<
      "2)Compute area (for all records)"<<endl<<
      "3)Print shapes report."<<endl<<   		
      "4)Sort shape data."<<endl;
       


}

int main()
{
	string crossCord,squareCord,rectCord,data;
	string shape,type;
	int choice,sortChoice=0;
	
	int counter=0;
	ShapeTwoD shape2D;
	Cross cross;
	Square square;
	Rectangle rect;
	ShapeTwoD *Cross = &cross;
	ShapeTwoD *Square = &square;
	ShapeTwoD *Rectangle = &rect;


        
	
	do
	{
	showProgram();
	cout<<"Please select your choice :";
	cin>>choice;
	


	switch(choice)
	{

	case 1 : 
		{
		 cout<<endl;
		 cout<<"[Input sensor data]"<<endl;
		 cout<<"Please enter name of shape :"<<endl;
		 cin>>shape;
		 shape2D.setName(shape);
		 cout<<"Please enter special type :"<<endl;
		 cin>>type;
		 shape2D.setWarpSpace(type);
		 cout<<shape2D.getName();
		 string shape=shape2D.getName()+","+shape2D.getWarpSpace()+",";	
		 if(shape2D.getName()=="Cross"||shape2D.getName()=="cross")
		 {
			
			cross.setCord(shape);
		        crossCord=cross.getCrossCord();
			
			
			
		 }else if(shape2D.getName()=="square" || shape2D.getName()=="Square")
		
		{
			
			square.setCord(shape);
			crossCord=square.getSquareCord();
					
		}
		else if(shape2D.getName()=="rectangle" || shape2D.getName()=="Rectangle")
		{
			
			rect.setCord(shape);
			crossCord=rect.getRectCord();
			

		}
		
		


		data=crossCord;
		
		arrayOfShapes[counter]=data;
		counter++;
		
		
		

		
		
		
		
		

		

		 cout<<"Record successfully stored. Going back to main menu...."<<endl;

		 break;
		 }	

	case 2:  //compute method
		 for(int i=0;i<counter;i++)
		 {
			
					
			
			//rect.setData(data);
			//cross.setData(data);
			//square.setData(data);
			stringstream area;
			float getArea;
		        stringstream toString(arrayOfShapes[i]);
			string shapeName;
			getline(toString,shapeName,',');
			

			if(shapeName == "Cross" || shapeName == "cross")
			{
			  
			  cross.setData(arrayOfShapes[i]);
			  getArea=cross.computeArea();
			  
			}
		       else if(shapeName == "Square" || shapeName =="square")
			{
			square.setData(arrayOfShapes[i]);
			getArea=square.computeArea();
			

			}
			
			else if(shapeName == "Rectangle" || shapeName == "rectangle")
			{
			  rect.setData(arrayOfShapes[i]);
			  getArea=rect.computeArea();
			}
			
			
			area<<getArea;
			arrayOfShapes[i]=area.str()+","+arrayOfShapes[i];
			cout<<arrayOfShapes[i]<<endl;	

		 }
		 
		 
		
		 	
		 cout<<"Computation completed!"<<endl;
		 break;


	case 3 :   cout<<"Total No. of Records avaliable = "<<counter<<endl;
		   
		for(int i=0;i<counter;i++)
		{
		string shapeName,warpSpace,point,area;
		stringstream toString(arrayOfShapes[i]);
		cout<<"Shape ["<<i<<"]"<<endl;
		getline(toString,area,',');
		getline(toString,shapeName,',');
		getline(toString,warpSpace,',');
		
		
		cout<<"Name:"<<shapeName<<endl;
		cout<<"Special Type :"<<warpSpace<<endl;
		cout<<"Area:"<<area<<endl;
		cout<<"Vertices"<<endl;
		
		if(shapeName=="Cross"||shapeName=="cross")
		{
		for(int b=0;b<12;b++)
		{
			
		getline(toString,point,'@');
		cout<<"Point["<<b<<"]"<<point<<endl;
		}
		   }
		else if(shapeName=="Square"||shapeName=="square")
		{
		for(int a=0;a<4;a++)
			{
		getline(toString,point,'@');
		cout<<"Point["<<a<<"]"<<point<<endl;
			}	
		}
		else if(shapeName=="Rectangle"||shapeName=="rectangle")
			{
				for(int c=0;c<4;c++)
				{
				getline(toString,point,'@');
				cout<<"Point["<<c<<"]"<<point<<endl;
				}
			}
		
		}

		break;


	case 4:  cout<<"1)	Sort by area (ascending)"<<endl;
		 cout<<"2)	Sort by area (descending)"<<endl;
		 cout<<"3)	SOrt by special type and area"<<endl;
		 cout<<"Please select sort option ('q' to go back to main menu) :";
		 cin>>sortChoice;
		do{
		switch (sortChoice)
		{

		case  1 :  sort(arrayOfShapes,arrayOfShapes+100);
			    for(int i=0;i<counter;i++)
			    {
				/*string shapeName,warpSpace,point,area;
				stringstream toString(arrayOfShapes[i]);
				cout<<"Shape ["<<i<<"]"<<endl;
				getline(toString,area,',');
				getline(toString,shapeName,',');
				getline(toString,warpSpace,',');
		
		
				cout<<"Name:"<<shapeName<<endl;
				cout<<"Special Type :"<<warpSpace<<endl;
				cout<<"Area:"<<area<<endl;
				cout<<"Vertices"<<endl;*/

				cout<<arrayOfShapes[i];
		

			    }
			  break;






		}
		 


		   }while(sortChoice !='q');

	}

	}while(choice !=5);




	return 0;


}
1
2
3
4
do
{
   // Your code
} while (sortChoice != 'q');


This is an infinite loop because there's nothing inside of the loop that can change the condition to prevent it from looping again.

sortChoice needs to change inside of that loop at some point, to stop it from looping.
@iHutch105
i have declared a case 'q' to break out of the loop.
However, the same thing happens. the switch case just hangs
when i keyed in 1 at line 232. It shows nothing when i asked the
program to print out the arrays
You are checking for integers, while inputting chars. So the 1, comes out as value 49. Add single quotes around the '1', and, of course, the rest of the cases in that section, when you write it.
@whitenite1
i have changed my selection cases
to 'a','b','c'

however, i tried a cout<<"hello word"<<endl;

the command prompt just repeats hello world in an infinite loop.

Could it be that i cannot have a nested do-while loop?

it puzzles me

edit: the loop occurs when i select choice 'a'
Last edited on
@CLearner88

Make one small change to your do/while. You have this..
1
2
3
4
5
cout<<"Please select sort option ('q' to go back to main menu) :";
		 cin>>sortChoice;
		do{
		switch (sortChoice)
		{
Change it to this.
1
2
3
4
 do{
    cout<<"Please select sort option ('q' to go back to main menu) :"; // Move line to here
    cin>>sortChoice; // and move this cin line, to here
        switch (sortChoice)
Oh it works, thank you so much! =)
Glad I could help.
Topic archived. No new replies allowed.