943,076 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1054
  • C++ RSS
Nov 29th, 2009
0

Nested Switch Statements

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kennych is offline Offline
12 posts
since Nov 2009
Nov 29th, 2009
0
Re: Nested Switch Statements
I'm sure there are more elegant solutions to this problem, but here's an example I thought of
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 29th, 2009
0
Re: Nested Switch Statements
I need to use both whiles though.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kennych is offline Offline
12 posts
since Nov 2009
Nov 29th, 2009
0
Re: Nested Switch Statements
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.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 29th, 2009
0
Re: Nested Switch Statements
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)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kennych is offline Offline
12 posts
since Nov 2009
Nov 30th, 2009
0

return exits the whole function

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
suho is offline Offline
12 posts
since Apr 2009
Nov 30th, 2009
0
Re: Nested Switch Statements
You are correct. I think the OP had that in error. What was suggested was return or break not both.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC