| | |
Nested Switch Statements
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 10
Reputation:
Solved Threads: 0
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.
Currently the current code exits the entire program after the second switch exits, which of course was not the plan.
C++ Syntax (Toggle Plain Text)
#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); }
Last edited by Kennych; Nov 29th, 2009 at 6:39 am.
•
•
Join Date: Sep 2009
Posts: 1,275
Reputation:
Solved Threads: 146
0
#2 Nov 29th, 2009
I'm sure there are more elegant solutions to this problem, but here's an example I thought of
C++ Syntax (Toggle Plain Text)
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 }
•
•
Join Date: Nov 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#5 Nov 29th, 2009
Update: I have fixed my issue using the help given by the poster who provided the idea. The code is below. Marking solved!
C++ Syntax (Toggle Plain Text)
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; } }
![]() |
Similar Threads
- Printing a song by using repetition and switch statements (C++)
- stacks problem (C++)
- Nested Select statements (Oracle)
- First Uni assignment having some trouble (C++)
- Paper Rock Scissors (C++)
- Help tweaking some java (JavaScript / DHTML / AJAX)
- If Or Switch (PHP)
- switch/case statement (C++)
Other Threads in the C++ Forum
- Previous Thread: compress a file with any extension and decompress the compressed...using c++
- Next Thread: syntax for initializing heap structure array?
Views: 604 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for c++
api array arrays assignment background based basic binary bitmap book books byte c++ calculator char char* cipher class classes client code compile compiler console constructor cout crashcourse currency delete desktop development dll download embedded embedding encryption error evc file fstream function functions game graph ifstream input int java keyboard library link linker linux loop macs math matrix microsoft newbie number numbers objects opengl output path pause pointer problem professor program programing programming projects python qt read recursion recursive richedit rpg send stream string strings struct studio subclass system template templates test text toolkit tree unicode university url visual win32 window







