Code Critique

closed account (35G3T05o)
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.




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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <cstdlib>
#include <iostream>

using namespace 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");
            }
        else if (play == 1){
            level_1();
            }
        else if (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");
        }
     else if (choice == 1){
          game_over();
     }
     else if (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");
        }
     else if (choice == 1){
          system("cls");
          cout << "You knock on the door but there is no answer\n\n";
          system("PAUSE");
          }
     else if (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");
          }
     else if (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");
        }
     else if (choice == 1){
        system ("cls");
        level_4();
        }
     else if (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();
        }
     else if (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");
         }
     else if (choice == 1){
         system("cls");
         cout << "She dosent seem to notice you\n\n";
         system("PAUSE");
         system("cls");
         }      
     else if (choice == 2){
         system("cls");
         cout << "She does not move\n\n";           
         system("PAUSE");
         system("cls");
         }
     else if (choice == 3){
         game_over();
         }
     else if (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();
          }
     else if (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);
}         
    











There's not much critique from my side. You can do it like so, but there're always other ways...

Do not call main()! It initializes certain global variables and you may face undefinded behaviour.

system() is mostly frowned upon, but I guess with small projects there should be no problem.
Look at the Loops Thread by SGM - I have put heaps of advice in there about how to use functions, switch statements Good ways to do Menus etc.

There are a lot of posts hopefully you can pick out some good stuff.

I am helping SGM to go through a reasonable design process. Most newbies miss out on this altogether.

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!
Last edited on
closed account (35G3T05o)
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.
Last edited on
switch statements are better than a load of else ifs, provided that the expression is constant.

I have been banging on, to everyone about not using do-whiles, use while loops instead.

DON'T CALL MAIN -IT'S A NIGHTMARE SCENARIO and is definitely not the way to restart a program.

Use a while loop in main, with a suitable test expression.

}while(play != 1 && play != 2);

In my personal opinion - I HATE THIS ALSO

Here is a function:

1
2
3
4
5
6
7
8
9
10
11
12
bool IsOperator(char symbol)
{
  switch (symbol)
  {
  case '+':
  case '-':
  case '*':
  case '/': return true;
  default:  return false;
  }
}


You could write something similar to test for good input.

Look on the loops thread by SGM to see how to use ShowMenu and ProcessMenu functions.

THere are a lot of posts on there and some confusion, but I hope you can pick out the right way to do things.

hope this helps - just trying to point people in the right direction to do their code well.
Topic archived. No new replies allowed.