954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Nested Switch Statements

Essentially what I'm trying do with nested statements is that when I enter the the second switch stament and when I exit the second switch statement it goes back to the first switch statement menu.

Currently the current code exits the entire program after the second switch exits, which of course was not the plan.

#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;
int main(int argc, char** argv) {
int ch1,ch2,tmp;
while(1)
{
cout<<endl<<endl;
cout<<" Menu                          "<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Select Directory "<<endl;
cout<<" 4. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch1;
switch(ch1)
{
   case 1 :{
       while(1) {
           cout<<" Directory Selection"<<endl;
            cout<<" 1. Computing Science "<<endl;
            cout<<" Enter your choice : ";
            cin>>ch2;
           switch(ch2){
               case 1 : break;
               case 4:
                    return 0;
                    break;
           }
       }
}
   case 4:
    return 0;
    break;
}
}

    return (EXIT_SUCCESS);
}
Kennych
Newbie Poster
13 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

I'm sure there are more elegant solutions to this problem, but here's an example I thought of

bool exitloop = false;
switch(ch1)
{
   case 1 :{
       while(1) {
           cout<<" Directory Selection"<<endl;
            cout<<" 1. Computing Science "<<endl;
            cout<<" Enter your choice : ";
            cin>>ch2;
           switch(ch2){
               case 1 : break; //out of inner switch
               case 4 :exitloop = true;  break; //out of inner switch
           }
        if(exitloop)
	   break;  //out of while
       }

   break;  //out of outer switch
	   }
jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

I need to use both whiles though.

Kennych
Newbie Poster
13 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This doesn't preclude that. In your outer one you exit from while via the return statement. This one, the break will only exit out one layer.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

Update: I have fixed my issue using the help given by the poster who provided the idea. The code is below. Marking solved!

int ch1,ch2,tmp;
while(1)
{
    bool exitloop = false;
cout<<endl<<endl;
cout<<" Menu "<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Select Directory "<<endl;
cout<<" 4. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch1;
switch(ch1)
{
case 1 : {
while(1) {
cout<<" Directory Selection"<<endl;
cout<<" 1. Computing Science "<<endl;
cout<<" Enter your choice : ";
cin>>ch2;
switch(ch2){
case 1 :
    break;
case 4:
    exitloop = true;
    break;
}
if (exitloop == true)
    break;
}
break;
}
case 4:
return 0;
break;
}
}
Kennych
Newbie Poster
13 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

return statement exits the whole function. In this case it is the main function.
I don't see a point in using break statements after 'return' , they just wont be executed.

suho
Newbie Poster
12 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

You are correct. I think the OP had that in error. What was suggested was return or break not both.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: