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);
}

Edited 7 Years Ago by Kennych: n/a

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
	   }

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.

Edited 7 Years Ago by jonsca: n/a

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;
}
}

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.

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

This question has already been answered. Start a new discussion instead.