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
#include <iostream>
#include <string>
usingnamespace 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;
}
}
}
}
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";
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
}