Easy Way To Do Switch Statements?

Write your question here.
I want to shorten my switch statements, or maybe if there is a different type
i should be using instead of switch. The code outputs correctly, Just looking to simplify
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string yourname;
	cout << "What is your name: ";
	getline(cin, yourname);
	if (yourname == "Thomas")
        cout << yourname << " is a weird name!\n";
    else
        cout << "Wow! " << yourname << " is a really cool name!\n";
	int direction;
	cout << "So... " << yourname << ", Choose a direction: Left or Right?\n" << "[1] Left, [2] Right: ";
	cin >> direction;
	string otherdirection = " ";
	switch (direction)
	{
	case 1:
		cout << "You have chosen to go Left!\n";
		otherdirection = "Right";
		cout << "As you are heading left you see a large dragon, it looks very hungry.\n";
		cout << "You should have went " << otherdirection << endl;
		break;
	case 2:
		cout << "You have chosen to go right!\n";
		otherdirection = "Left";
		cout << "As you are heading right you see a large dragon, it looks very hungry.\n";
		cout << "You should have went " << otherdirection << endl;
		break;
	default:
		cout << "Where exactly are we going?" << endl;
	}
	int battle;
	cout << "Will you fight the dragon\?" << "\n[1]Yes [2]No: ";
	cin >> battle;
	switch (battle)
	{
	case 1:
		cout << "You fight the dragon!\n";
		cout << "You are instantly killed.";
		system("Pause");
		return 0;
		break;
	default:
		cout << "You hide behind a rock!\n";
		cout << yourname << " Wins!\n";
		cout << "You find a sword on the ground.\n";
			int sword;
			cout << "will you pick up the sword\?";
			cout << "[1] Yes [2] No: ";
			cin >> sword;
			switch (sword)
			{
			case 1:
				cout << "You poke your eye out!\n";
				cout << "You are instantly killed.";
				system("Pause");
				return 0;
				break;
			default:
				cout << "What are you afraid of\?\n";
				cout << yourname << " Is lame! ";
				int lame;
				cout << "are you going to take that\?";
				cout << "[1] Yeah... [2] No!!!: ";
				cin >> lame;
				switch (lame)
				{
				case 1:
					cout << "You took that to heart!\n";
					cout << "You have a heart attack.";
					system("Pause");
					return 0;
					break;
				default:
					cout << "You stand up for yourself!\n";
					cout << yourname << " is not actually lame! ";
					system("Pause");
					return 0;
					break;
				}
			}
	}
}
Last edited on
closed account (48T7M4Gy)
While it isn't a switch control, here is a simple example where you can take better advantage of the C++ tools and make your code read a little better. Also it helps you focus on alternative paths:

Instead of:
1
2
3
4
5
6
7
8
if (yourname == "Thomas")
	{
		cout << yourname << " is a weird name!\n";
	}
	if (yourname != "Thomas")
	{
		cout << "Wow! " << yourname << " is a really cool name!\n";
	}


Why not just:

1
2
3
4
if (yourname == "Thomas")
   cout << yourname << " is a weird name!\n";
else
   cout << "Wow! " << yourname << " is a really cool name!\n";
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch (direction)
	{
	case 1:
		cout << "You have chosen to go Left!\n";
		otherdirection = "Right";
		cout << "As you are heading left you see a large dragon, it looks very hungry.\n";
		cout << "You should have went (sic) " << otherdirection << endl;
		break;
	case 2:
		cout << "You have chosen to go right!\n";
		otherdirection = "Left";
		cout << "As you are heading right you see a large dragon, it looks very hungry.\n";
		cout << "You should have went (sic) " << otherdirection << endl;
		break;
	default:
		cout << "Where exactly are we going?" << endl;
		// etc etc error
	}
Topic archived. No new replies allowed.