How can i exist nosted "switch" without existing from the program (C++)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i exist nosted "switch" without existing from the program (C++)?

hi guys , i need help please i wrote a c++ program for my exam test , in this program i used a nosted "switch " how can i exist from the interior "switch" and go to the exterior "switch" without exiting from the program? Simple example : while(j!=2) { cin>>j ; switch(j) { case 1 : while(d!=2) { cin>>d ; switch(d) { case 1 : cout<<"hi"<<endl; case 2 : cout<<"hello"<<endl ; } } case 2 : cout<<"money " ; } } For example i chose the first case of the interior "switch" After the result appears, I want to return to the exterior "switch "

18th May 2021, 12:45 AM
Ramzi Jebalia
Ramzi Jebalia - avatar
5 Answers
0
Thirt13n my problem is not the declaration , i mean for example i chose the case 1 of the exterior switch , then the case 1 of the interior switch , he return hi The i want to return to the exterior switch and chose case 2 he return money
18th May 2021, 12:55 AM
Ramzi Jebalia
Ramzi Jebalia - avatar
+ 1
hey Ramzi Jebalia if your while loop satisfy condition then it goes to infinity ! I suggest do not use while loop ! well you do something like this .... sol: use goto label inner switch's case:1 , label it before external switch's case 2, then use return statement after cout; switch(d) { case 1: cout<<blabla; goto some_label; case 2: cout<< ;} } some_label : case 2 : cout<<"money"; return 0; }
18th May 2021, 1:08 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
0
Ramzi Jebalia depends on j and d , but you have to declare variable before using them !
18th May 2021, 12:51 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
0
Thirt13n thank youuu , i will try it🙏
18th May 2021, 1:10 AM
Ramzi Jebalia
Ramzi Jebalia - avatar
0
That's what "break" statement is for. while(j!=2) { cin>>j ; switch(j) { case 1 : while(d!=2) { cin>>d ; switch(d) { case 1 : cout<<"hi"<<endl; break; case 2 : cout<<"hello"<<endl ; break; } } case 2 : cout<<"money " ; } } and as far as "goto" statement is considered, I would recommend avoid using it as much as you can to avoid transforming your program to an unmanageable mess.
18th May 2021, 6:44 AM
Arsenic
Arsenic - avatar