Nested Switch Statements

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Thread Solved

Join Date: Nov 2009
Posts: 10
Reputation: Kennych is an unknown quantity at this point 
Solved Threads: 0
Kennych Kennych is offline Offline
Newbie Poster

Nested Switch Statements

 
0
  #1
Nov 29th, 2009
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.
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. int main(int argc, char** argv) {
  7. int ch1,ch2,tmp;
  8. while(1)
  9. {
  10. cout<<endl<<endl;
  11. cout<<" Menu "<<endl;
  12. cout<<" ----------------------------- "<<endl;
  13. cout<<" 1. Select Directory "<<endl;
  14. cout<<" 4. Exit "<<endl;
  15. cout<<" Enter your choice : ";
  16. cin>>ch1;
  17. switch(ch1)
  18. {
  19. case 1 :{
  20. while(1) {
  21. cout<<" Directory Selection"<<endl;
  22. cout<<" 1. Computing Science "<<endl;
  23. cout<<" Enter your choice : ";
  24. cin>>ch2;
  25. switch(ch2){
  26. case 1 : break;
  27. case 4:
  28. return 0;
  29. break;
  30. }
  31. }
  32. }
  33. case 4:
  34. return 0;
  35. break;
  36. }
  37. }
  38.  
  39. return (EXIT_SUCCESS);
  40. }
Last edited by Kennych; Nov 29th, 2009 at 6:39 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1,275
Reputation: jonsca is just really nice jonsca is just really nice jonsca is just really nice jonsca is just really nice 
Solved Threads: 146
Sponsor
jonsca jonsca is online now Online
Nearly a Posting Virtuoso
 
0
  #2
Nov 29th, 2009
I'm sure there are more elegant solutions to this problem, but here's an example I thought of
  1. bool exitloop = false;
  2. switch(ch1)
  3. {
  4. case 1 :{
  5. while(1) {
  6. cout<<" Directory Selection"<<endl;
  7. cout<<" 1. Computing Science "<<endl;
  8. cout<<" Enter your choice : ";
  9. cin>>ch2;
  10. switch(ch2){
  11. case 1 : break; //out of inner switch
  12. case 4 :exitloop = true; break; //out of inner switch
  13. }
  14. if(exitloop)
  15. break; //out of while
  16. }
  17.  
  18. break; //out of outer switch
  19. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 10
Reputation: Kennych is an unknown quantity at this point 
Solved Threads: 0
Kennych Kennych is offline Offline
Newbie Poster
 
0
  #3
Nov 29th, 2009
I need to use both whiles though.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1,275
Reputation: jonsca is just really nice jonsca is just really nice jonsca is just really nice jonsca is just really nice 
Solved Threads: 146
Sponsor
jonsca jonsca is online now Online
Nearly a Posting Virtuoso
 
0
  #4
Nov 29th, 2009
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.
Last edited by jonsca; Nov 29th, 2009 at 7:30 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 10
Reputation: Kennych is an unknown quantity at this point 
Solved Threads: 0
Kennych Kennych is offline Offline
Newbie Poster
 
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!

  1. int ch1,ch2,tmp;
  2. while(1)
  3. {
  4. bool exitloop = false;
  5. cout<<endl<<endl;
  6. cout<<" Menu "<<endl;
  7. cout<<" ----------------------------- "<<endl;
  8. cout<<" 1. Select Directory "<<endl;
  9. cout<<" 4. Exit "<<endl;
  10. cout<<" Enter your choice : ";
  11. cin>>ch1;
  12. switch(ch1)
  13. {
  14. case 1 : {
  15. while(1) {
  16. cout<<" Directory Selection"<<endl;
  17. cout<<" 1. Computing Science "<<endl;
  18. cout<<" Enter your choice : ";
  19. cin>>ch2;
  20. switch(ch2){
  21. case 1 :
  22. break;
  23. case 4:
  24. exitloop = true;
  25. break;
  26. }
  27. if (exitloop == true)
  28. break;
  29. }
  30. break;
  31. }
  32. case 4:
  33. return 0;
  34. break;
  35. }
  36. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: suho is an unknown quantity at this point 
Solved Threads: 0
suho suho is offline Offline
Newbie Poster

return exits the whole function

 
0
  #6
Nov 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1,275
Reputation: jonsca is just really nice jonsca is just really nice jonsca is just really nice jonsca is just really nice 
Solved Threads: 146
Sponsor
jonsca jonsca is online now Online
Nearly a Posting Virtuoso
 
0
  #7
Nov 30th, 2009
You are correct. I think the OP had that in error. What was suggested was return or break not both.
Reply With Quote Quick reply to this message  
Reply

Tags
c++

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 604 | Replies: 6
Thread Tools Search this Thread



Tag cloud for c++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC