# Small “Pyramid Escape” text-based game

I'm extremely amateur when it comes to C++ in general, but I wanted to make a little "Pyramid Escape" text based game. I'm not done whatsoever but before I continue I want to see what I could possible do differently.

//
//  main.cpp
//  TextFiles
//
//  Created by Neal Carico on 3/12/15.
//

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, const char * argv[]) {

string start;
string restart;
int turn1;
int turn2;
int turn3;
int turn4;
string decision1;
string decision2;
string decision3;
string decision4;
string decision5;
int random1;
int random2;
int random3;

while(restart != "dontrestart" || dead != "false"){
cout << "Welcome to the worlds largest pyramid. . . Type 'start' to enter or 'leave' to walk away..." << endl;
cin >> start;

///The Start
if (start == "start"){

///The main part of the game.

cout << "Oh no! The pyramid collapsed trapping you in. You are the only one alive. . ." << endl;

int score=0;

cout << "Okay. So you have two options here. . .Left(1) or right(2)?" << endl;
cin >> turn1;
if(turn1 == 2){
cout << "BAM! The roof collapsed over you and you die. Sad stuff" << endl;
cout << "Thank you for playing. Your score was " << score << endl;
exit(1);
}
cout << "You took the left path. And then BAM! The path to the right collapsed... lucky you." << endl;
score++;
cout << "There gold on the ground.. do you pick it up?[y/n]" << endl;
cin >> decision1;

if(decision1 == "y"){
cout << "You greedy bastard you. Lucky I like the irony and am not going to kill you in this instance... You find 10 gold." << endl;
score = score + 10;
}

cout << "You start heading forward again and come to another turn. But this time there are actually four options... :)" << endl;
random1 = rand() % 4 +1;
cout << "Your options are as follows. 1) Up to right 2)Down to right 3)Up to left and 4)Down to left. This one changes everytime ;). The random number is " << random1 << endl;
cin >> turn2;
if (turn2 == random1) {
cout << "BAM! The roof collapsed over you and you die. Sad stuff" << endl;
cout << "Thank you for playing. Your score was " << score << endl;
exit(1);
}
cout << "Sweet. You made it. There's a point." << endl;
score++;

cout << "Okay you start walking down the tunnel and something is moving in the distance. Do you approach it or head back to one of the other tunnels?(continue/return" << endl;
cin >> decision2;
if(decision2 == "continue"){
cout << "Yup, I'm not nice enough to give you a break 2 times in a row. Dead." << endl;
cout << "BLAAAHHHGGGG!! The figure is a crazed mummy who attacks you and you die. Sad stuff" << endl;
cout << "Thank you for playing. Your score was " << score << endl;
exit(1);
}
cout << "There is a screeching sound as you turn around." << endl;
score++;

///End of game stuff

}else if(start == "leave"){
cout << "You walked away and heard a huge BOOM and saw the pyramid collapsed trapping the tour group in. You are" <<
" one lucky person. Sure you lived, but you didn't even play the game. How lame.You can type 'restart' to try again, but you're always going to be a loser to me." << endl;
}else{
cout << "Error. Please type restart or 'dontrestart' (notice there are no spaces) to exit the game..." << endl;
}
cin >> restart;
if (restart !="restart"){
exit(1);
}

}
return 0;

}

• Objects would be good. See this answer to a similar question. – Edward Apr 7 '15 at 20:26

First, don't use using namespace std;, or any other namespace, for that matter. This can cause you have load functions with the same name, which can cause problems. This is discussed in more detail here.

Second, you have a ton of variables you never use, including:

int turn3;
int turn4;
string decision3;
string decision4;
string decision5;
int random2;
int random3;


Third, you should use move functions. One candidate for a function is asking whether to restart the game.

Fourth, I would use bools for dead and restart. You will need to convert the input for restart, but it better represents what is happening.

Fifth, you should consistently indent your code at 4 spaces per level. You do this for the most part, but you do not do this in main().

• I saw on some other code that I shouldn't use namespaces, but they seem so helpful! As for the unused variables, I plan to use them eventually. Which is why I said it's not done. Thanks for your comments. I'll look into adding functions. – NealC Apr 7 '15 at 20:06
• But, one question. Would it really be more efficient to add "std::" to all my cout and cin commands? – NealC Apr 7 '15 at 20:25
• @NealC I don't know about efficient, but much safer. If you start having ambiguous calls, you will have to do this anyway so the system knows which one to call. – Hosch250 Apr 7 '15 at 20:37
• There is not a problem with using namespace std in a program you write as long as you do NOT use it in a header file and as long as no other namespace you use contains conflicting names. If you just want to import specific items use can do so as with using std::cout. – Edward Apr 7 '15 at 20:42
• @NealC Listen to Edward. He is better at this than I. – Hosch250 Apr 7 '15 at 20:46