switch inside while loop not stopping at break

so I tried making my first game, it is supposed to be a text-based multi-option game but it doesn't stop when it should. this is the best way I could figure out how to make what I wanted.

#include <iostream>

using namespace std;

int main()
{
int choise = 0;
int place = 0;
int ending =0;

while (ending < 1){

switch (place){
case 0:// place

cout << "you wake up to the smell of fire, what do you do?" << endl;
cout << " 1 go back to sleep \n 2 rush towards door \n 3 rush towards window \n" << endl;

cin >> choise;

switch (choise){
case 1:

cout << "that was stupid. \nrestarting game\n" << endl;
place = 0;
break;

case 2:
cout << "you rush towards the door\n" << endl;

place = 1;
break;

case 3:
cout << "you rush towards the window\n" << endl;

place = 2;
break;

default:
cout << "invalid number \ntry again\n" << endl;

place = 0;
break;

}

break;
case 1:// place

cout << "you reach the door, what do you do?" << endl;
cout << " 1 open door and leave \n 2 return to the bed\n" << endl;
cin >> choise;

switch (choise){
case 1:
cout << "you leave the room and escape safely" << endl;
ending = 1;
break;

case 2:
cout << "you return to the bed\n" << endl;
place = 0;
break;

}

case 2:// place

cout << "you reach the window, what do you do" << endl;
cout << " 1 leave trough the window \n 2 return to the bed" << endl;
cin >> choise;

switch (choise){
case 1:
cout << "you climb trough the window and escape safely" << endl;
ending = 1;
break;

case 2:
cout << "you return to the bed\n" << endl;
place = 0;
break;

}

break;

}
}

return 0;
}

if you can't help me solve my problem maybe guide me to a tutorial that can help me make it differently.

grateful for answers.
closed account (48bpfSEw)
what do you mean with "it doesn't stop when it should". I could escape safely out of the window! ^^ and the programm stopped.


this is a way I would create a text game:

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
#include <map>
#include <string>

int main() {

  typedef map<string, string>   MapGame;         // game relations 
  typedef map<string, string>   MapDescription;  // object descriptions 
  typedef map<string, string>   MapCommand;      // commands (user input)

  typedef MapGame::iterator          ItMapGame;
  typedef MapDescription::iterator   ItMapDescription;
  typedef MapCommand::iterator       ItMapCommand;


  MapGame           g;    // game relations 
  MapDescription    d;    // object descriptions 
  MapCommand        c;    // commands (user input), prerequisits and actions (workflow)


  // A simple game: 
  // There is a room, a user, a key and a door. 
  // The User have to "get the key" and "open the door".
  
  g["game"] = "started";          // game state
  g["room"] = "user;key;door";    // room has user, key and door 
  g["door"] = "closed";           // door is closed (state)
  
  d["room"] = "This is the room where you woke up this morning.";
  d["door"] = "The room has a door where you can escape!";
  d["key" ] = "This is a key."
  d["user"] = "You are the user trying to find the way out."
    
  // Pseudo-Code  
    
  //  if command is one of "open door;unlook door" then 
  //    check condition "user has key"
  //    check condition "door is closed"
  //      if all conditions ok 
  //        then "door is open" and "game is finished"
  c["open door"] = "[user key;door closed][door open;game finished]";
  c["unlook door"] = "ref: open door"]
  
  //  if command is on of "get key;take key" then 
  //    check condition "room has key"
  //      if all conditions ok 
  //        then "user has key", delete "room has key"
  c["get key"  ] = "[room key][user key from room]"; 
  c["take key" [ = "ref: get key";
  
  //  if command is on of "look around;where i am" then 
  //    find all relations to "user" and show the descriptions
  //      example: room-user : cout << d["room"] << endl;
  //               room-key  : cout << d["key"] etc.
  c["look arround"  ] = "describe"; 
  c["where i am"    ] = "ref: look arroung";
  
  while (true) {
    ItMapGame itG = g.find("game");
    if (itG == g.end())
      return -1;   // assert
    if (itG->second == "finished")
      return 1; // success
    
    string strInput;
    cin >> strInput;

    ItMapCommand itC = c.find(strInput);
    if (itC == c.end()) {
      cout << "I am sorry?" << endl;
      continue;
      }
  
    // PseudoCode:
    string strCommand = itC->second;
    
    // split strCommand in 2 strings, enclosed in [ ... ]
    //  e.g. strPreReq = "user key;door closed"
    //       strAction = "door open;game finished"
  
    // check the conditions in strPreReq (splitting again in : "user key" and "door closed"
    // e.g. itG = g.find("user"); 
    //      if (itG == g.end()) return -1; // assert
    //      if (itG->second != "key") { cout << "prereq. failed. user has not the key to open the door!"; continue; }    
    
    // if the code is here, all conditions were fullfilled
    // Now do strAction : split string in "door open", "game finised"
    //   itG = g.find("door");  // check assert ...
    //   itG->second = "open";  cout << "door open!" << endl;
    //   itG = g.find("game");  // check assert ...
    //   itG->second = "finished"; cout << "game finished!" << endl;
  
    }
  return 0;
  }
Last edited on
what I'm saying is, if you choose "rush towards door" and then choose "open door and leave" it says

"you rush to the window"

or is that just my compiler?

thankful for the answer btw

that is a good game but I was looking to make more of a story game.
where you choose different routes and get different results.
Last edited on
I think you should use a switch for the place inside that switch...

am I wrong ?
an I not?

is it not a switch inside of the "place" case 1?
that sais "

case 1:
cout << "you leave the room and escape safely" << endl;
ending = 1;
break;

case 2:
cout << "you return to the bed\n" << endl;
place = 0;
break;
"

or am I simply not understanding what you're saying?
closed account (48bpfSEw)
1
2
3
4
        case 1: // place
...
BREAK;
        case 2: // place 


you forgot the break after case 1!

OH! thanks alot
Topic archived. No new replies allowed.