Switch statements w/strings

Is there any way to do something like the following:
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
103
104
105
106
107
#include<iostream>
#include<string>
using namespace std;

int main()
{
  
   string action, item1, item2;
   int progress = 0;
   bool a = true;
   system("cls");
   cout << "You are in a jail cell, for reasons irrelevant. You are trying to escape. Items and such: (a) thin brick wall dividing cells, (b) a wooden door with unbreakable lock, (c) bed, (d) firm board. Press n for \"need help\", or the appropriate \naction button to act.\n";
   do{
   getline(cin, action);
   switch (action)
   {
   case 'n':
     cout << "Type u or use to use an item on/with another item, l then the item's letter/name to examine it (for example, look at bed, or l at c), or x for exit. ";
     break;
   case 'u':
     cout << "Use what? ";
     cin >> item1;
     cout << "On what? ";
     cin >> item2;
     if(item1 == 'd' && item2 == 'c' && progress == 0)
     {
       cout << "You break off (f) a long, rigid piece of metal. ";
       progress++;
       }
     else if(item1 == 'board' && item2 == 'bed' && progress == 0)
     {
       cout << "You break off (f) a long, rigid piece of metal. ";
       progress++;
       }
     else if(item1 == 'f' && item2 == 'a' && progress == 1)
       {
       cout << "After a minute of battering the wall, you break into a cell, and take (g) an iron candlestick, (h) a flint piece, and (i) an unlit torch. ";
       progress++;
       }
     else if(item1 == 'metal' && item2 == 'wall' && progress == 1)
       {
       cout << "After a minute of battering the wall, you break into a cell, and take (g) an iron candlestick, (h) a flint piece, and (i) an unlit torch. ";
       progress++;
       }
     else if(item1 == 'h' && item2 == 'g' && progress == 2)
       {
         cout << "You light the (i) torch. ";
         progress++;
       }
     else if(item1 == 'flint' && item2 == 'candlestick' && progress == 2)
       {
         cout << "You light the (i) torch. ";
         progress++;
       }
     else if(item1 == 'i' && item2 == 'b' && progress == 3)
       {
       a = false;
       cout << "You burn down the door, and escape the jail! ";
       }
     else if(item1 == 'torch' && item2 == 'door' && progress == 3)
       {
       a = false;
       cout << "You burn down the door, and escape the jail! ";
       }
       else
         cout << "You can't do that. ";
       break;
       case 'look at wall':
       cout << "A thin brick wall, probably separating cells. ";
       break;
       case 'look at door':
       cout << "A rigid, dry wood door, with a magically sealed lock. The door cannot be busted down. ";
       break;
       case 'look at bed':
       cout << "Your bed, with metal bars on the underside. ";
       break;
       case 'look at board':
       cout << "A strong board, which could be used as a crowbar. ";
       break;
       case 'look at metal':
       cout << "A piece of rather thick metal, broken off of your bed. ";
       break;
       case 'look at candlestick':
       cout << "A candlestick made of iron. The wax was burned away long ago. ";
       break;
       case 'look at flint':
       cout << "A small piece of broken flint. ";
       break;
       case 'look at torch':
       if(progress == 2)
       cout << "An unlit torch, not even used. ";
       if(progress == 3)
       cout << "A lit torch, which should last long. ";
       break;
       case 'x':
       a = false;
       break;
       default:
       cout << "I do not understand that. Try a different syntax. ";
       break;
       }
}while(a);
   cin.get();
   cout << "Press ENTER to exit...\n";
   cin.get();
   return 0;
}

When I try to compile it, it has about 10 errors and 10 warnings. Any ideas?
Last edited on
You need to make action a char rather than a string, then it can be used in a switch statement. Since you are only wanting single character commands, this should be fine.
If you needed to handle more complicated commands (words such as 'look', 'up', 'use') then a possible approach would be to define an array of strings with all your commands in it. You could then find the index of a command when entered, and switch on the index.
aaaaaaactually....
I am a beginner. I haz no idear hou 2 do dat.
I'll look it up.
Topic archived. No new replies allowed.