Loop inside a switch

Hello all, I'm trying to make a switch case that repeats itself if it ends with default.

i.e.

1
2
3
4
5
6
7
8
9
case 1:
/*condition*/
break;
case 2:
/*condition*/
break;
default:
/*Some kind of loop to allow user to enter another choice.*/
break;


I'm probably missing something simple here. I'm trying to teach myself C++ cause school is too expensive.

Thanks in advance for any and all help.
Hi!


You should use a loop. While, do loop.
http://bytes.com/serversidescripting/c++/tutorials/c++andloops/index.html

like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

...
int main()
{
 int opt = 0;
 do
 {
   cout << "Enter your choice:";
   cin >> opt;
   switch(opt)
   {
    ...
   }
 }
  while (opt != 0); // stopping condition



 return;
}

...





I don't know it is free or not:

http://www.mediafire.com/?od11hmjdvwd

(teachyourselfcplusplusin21days) This book has 700 pages.

Hi, I don't know if you mean something like this but it works for me :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

int main()
{
    int num1 = 0;
    bool repeat_switch = true;

    while(repeat_switch != false)
    {
        std::cout << "Enter an integer between 1 - 5." << std::endl;
        std::cin >> num1;

        switch(num1)
        {
            case 1:
            std::cout << "You entered 1!" << std::endl;
            repeat_switch = false;
             break;

            //..... case 2 - 5

            default:
            std::cout << "Invalid input, enter a number 1 - 5 again." << std::endl;
             break;
        }
        //if the number isn't 1 - 5 then the condition for the loop (repeat_switch) will always be     
        //true  and therefore the switch will keep repeating itself. If it is, it'll turn false and the 
       //while(...) will end
    }

    return(0);
}


You can edit the cin >> to filter out results (see: firedraco's link) , but I just did it quickly.
Last edited on
That will mostly work fine. Read this for more info:
http://www.cplusplus.com/forum/articles/6046/

Also, returning from main is GOOD practice. Please keep doing it.
Thank you all for responding. Sorry I'm a little slow on returning. I'm making a Pick Your Own Adventure game, which is what I need the loop solution for. Anyone care to have a look at it so far?
Sure, I wouldn't mind (although I might have to do this tommorow, it's quite late (like after midnight late) for me. XD
Awesome. It will be nice to have some feedback. There is some bad language and some very dry humor, just warn you all. Sorry if the code is messy; again, self taught.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

string name;/* The player's name.*/

int main(int argc, char *argv[])
{
 int choice; /*The choice for each scenario.*/
 
  cout << "Hey. Yes you. It's a black screen and you." << endl;
  cout << "Who else would I possibly talking to?" << endl;
  cout << "Anyway, what is your name? " ;
  cin >> name;
  
  cout << endl;/*Blank line. Need to find better method.*/
  
  cout << name << " huh? " << endl;
  cout << "Well let's get started " << name << "." << endl; 
  
  cout << endl;
/*Question 1*/
  cout << "You are in a dimly lit room." << endl;
  cout << "You look around a notice doors on either side of you." << endl;
  cout << "Which one do you pick?" << endl; 
  cout << "1) Door on the left." << endl;
  cout << "2) Door on the right." << endl;
  cout << "Enter a choice: ";
  cin >> choice;
  
  cout << endl;
  
  switch (choice) {
   case 1:/*Correct choice to Question 1. Leads to Question 2.*/
    cout << "The door leads to a hallway, with the same poor lighting." << endl;
    cout << "You reach the end of the hallway and come to a door." << endl;
/*Question 2*/
    cout << "You open the door, and on the other side is a room with a guard." << endl;
    cout << "What do you do?" << endl;
    cout << "1) Run in and beat him over the head." << endl;
    cout << "2) Sneak up and snap his neck like a twig." << endl;
    cout << "Enter a choice: ";
    cin >> choice;
                    
    cout << endl;
                    
     switch (choice) {
     case 1:/*Incorrect choice to Question 2.*/
      cout << "The guard hears you running and screaming like an idiot..." << endl;
      cout << "...He impales you. You died. Good job." << endl;
      break;
     case 2:/*Correct choice to Question 2. Leads to Question 3.*/
      cout << "You sneak up quietly and say (insert cliche remark) before snapping his neck." << endl;
/*Question 3*/
      cout << "You take the gaurd's sword. You have a feeling you'll need it." << endl;
      cout << "You hear strange noises down a corridor at the end of the room." << endl;
      cout << "At the other end of the room is a window you can climb out of and escape." << endl;
      cout << "So, where do you go from here?" << endl;
      cout << "1)Jump out the window. Screw this place. There's better adventures elsewhere." << endl;
      cout << "2)Well any good adventurer investigates the noises right?" << endl;
      cout << "You know how this works by now. ";
      cin >> choice;
      cout << endl;
      switch (choice) {
      case 1:/*Incorrect choice to Question 3.*/
       cout << "You jump out the window and land right safely on the ground." << endl;
       cout << "Unfortunately for you, there are six guards practicing their archery." << endl;
       cout << "You get filled full of arrows. They try to bury you later but you're" << endl;
       cout << "stuck to that tree something awful." << endl;
       break;
      case 2:/*Correct choice to Question 3. Leads to Question 4.*/
       cout << "You head towards the corridor to see what's going on." << endl;
       cout << "You walk for what feels like hours." << endl;
       break;
      default:
       cout << "How about a choice that we can actually use, huh?" << endl;
       break;
       } 
      break;
     default:
      cout << "1 or 2 stupid." << endl;
      break;
      }
      break;
     case 2:/*Incorrect choice to Question 1.*/
      cout << "The door leads to a well-lit feast hall." << endl;
      cout << "You take one step into the room and...fall down a pit full of ravenous" << endl;
      cout << "squirells." << endl;
      cout << "Good job stupid. You killed yourself already." << endl;
      cout << "Now you'll never get the cookies at the end." << endl;
      break;
     default:
      cout << "Hey dumbass, You only have to type 1 or 2." << endl;
      cout << "Is it really that damn hard?" << endl;
      break;
           
  }                               
  system("PAUSE");	
  return 0;
}
Don't use endl for creating new line as it's flushes the stream, use '\n' instead and use endl only once/output.
Last edited on
Ah, thanks. I'll clean up now. All the endls were bugging me. I new there was a better way. What did you think of the actual game?
Heh, not bad. Some other things I noticed were:

1.) You could move name into main since you don't need it to be global.
2.) Check that article I mentioned to make sure your program doesn't break if someone enters invalid input
3.) Don't use system(), read here for more info: http://www.cplusplus.com/forum/articles/11153/
Thanks again firedraco. I have music composed too, but I'm saving that until the program is actually where I want it in terms of being done. Did you happen to find any grammatical/spelling errors? Any jokes to cheesy?

edit: screw, thanks for the link to that ebook. it was free, and it looks fantastic.
Last edited on
Topic archived. No new replies allowed.