Hi all, I'm new to C++ and am trying to teach myself. I've created a simple
(and cheesy) text adventure. Im hoping someone would be willing to take a look at my code and tell me how to improve my coding style and development approach.
The code runs as intended but I'm sure there is much room for improvement.
#include <cstdlib>
#include <iostream>
usingnamespace std;
void game_over();
void level_1();
void level_2();
void level_3();
void level_4();
int main(){
cout << " Welcome to the Haunted House" << endl << endl; // Title
int play;
do { // present choice untill valid number is chosen
cout << "Would you like to play?\n1.)Yes\n2.)No\nChoice: " ;
cin >> play;
if (!cin) { // discard chars
cin.clear();
cin.ignore(1000, '\n');
cout << "\nPlease enter a number." << endl << endl;
system("Pause");
system("cls");
}
elseif (play == 1){
level_1();
}
elseif (play == 2){
game_over();
}
}while(play != 1 && play != 2);
return 0;
}
void game_over(){ // function for when a player dies
system("cls");
cout << "Game Over\n";
system("PAUSE");
system("cls");
main(); // restarts program
}
void level_1(){
int choice;
do{
system("cls");
cout << "You are walking down the street and see a spooky house on a hill top. \n";
cout << "What would you like to do? \n1.) Keep walking \n2.) Get a closer look\nChoice: " ;
cin >> choice;
if (!cin){
cin.clear();
cin.ignore(1000, '\n');
cout << "Please enter a number" << endl << endl;
system("PAUSE");
system("cls");
}
elseif (choice == 1){
game_over();
}
elseif (choice == 2){
level_2(); // correct choice sends to next level
}
}while (choice != 1 && choice != 2);
}
void level_2(){
int choice;
do{
system("cls");
cout << "As you approach the door you notice a light on in the window.";
cout << "\nWould you like to: \n1.) Knock on the door \n2.) Peek in the window \n3.) Attempt to open the door \nChoice: ";
cin >> choice;
if (!cin){
cin.clear();
cin.ignore(1000, '\n');
cout << "Please enter a number" << endl << endl;
system("PAUSE");
system("cls");
}
elseif (choice == 1){
system("cls");
cout << "You knock on the door but there is no answer\n\n";
system("PAUSE");
}
elseif (choice == 2){
system("cls");
cout << "You see a long haired woman at the end of a hallway staring at the \nceiling.\n\n";
system("PAUSE");
}
elseif (choice == 3){
system("cls");
cout << "You open the door\n\n";
system("PAUSE");
system("cls");
level_3();
}
}while(choice !=3);
}
void level_3(){
int choice;
do{
cout << "You see a doorway leading into the hall with the light on, a staircase leading \nupstairs, and a shut door to the basement. ";
cout << "Would you like to: \n1.) Examine the hall \n2.) Walk up the stairs \n3.) Go in the basement \nChoice: ";
cin >> choice;
if (!cin){
cin.clear();
cin.ignore(1000, '\n');
cout << "Please enter a number" << endl << endl;
system("PAUSE");
system("cls");
}
elseif (choice == 1){
system ("cls");
level_4();
}
elseif (choice == 2){
system("cls");
cout << "You experience horrors that your mind cannot fathom and die a horrible and \npainful death \n\n";
system("PAUSE");
system("cls");
game_over();
}
elseif (choice == 3){
system("cls");
cout << "The door is locked" << endl << endl;
system("PAUSE");
system("cls");
}
}while (choice != 1);
}
void level_4(){
int choice;
do{
cout << "You walk in the hallway and see a woman but she does not notice you. She is \nstaring intently at the ceiling\n";
cout << "What do you do: \n1.) Try and speak to her \n2.) Tap her on the shoulder \n3.) Run away \n4.) Walk past her \n5.) Quickly strangle her to death \nChoice:";
cin >> choice;
if (!cin){
cin.clear();
cin.ignore(1000, 'n');
cout << "Please enter a number" << endl << endl;
system("PAUSE");
system("cls");
}
elseif (choice == 1){
system("cls");
cout << "She dosent seem to notice you\n\n";
system("PAUSE");
system("cls");
}
elseif (choice == 2){
system("cls");
cout << "She does not move\n\n";
system("PAUSE");
system("cls");
}
elseif (choice == 3){
game_over();
}
elseif (choice == 4){
system("cls");
cout << "As you walk past her she grabs you by the neck with superhuman strenth and \nstrangles you to death \n\n";
system("PAUSE");
system("cls");
game_over();
}
elseif (choice == 5){
system("cls");
cout << "You killed that evil bitch! Good Job! \nYou Win!!!!!!!!!!!!!\n\n";
system("PAUSE");
system("cls"); // Win game and exit program
}
}while (choice!= 5);
}
I don't know whether it is your intention, but this program never quits: when you choose 'no' at the main menu, it shows first gameover and then the main menu again. You can try to fix that. Good luck!
Thanks for feedback guys. Opinions of people more experienced makes all the difference. As I ran it a few more times, calling main() in the game_over() func
does indeed cause problems. I need to find a way to scratch current progress and restart the program. Currently trying to solve the problem with a do- while loop to no avail.