page handler

I have been creating a game on c++ for a few months now and have had quite a bit of help form my teacher and fellow c++ programmers, and hi have been told my script for the text is quite...messy...and have been looking into page handlers and I've only come across one example and it limits what i want to do on my pages by making it so i have to write around 5 lines of code for it to allow certain functions to work with it, which i'd like to avoid.
if anyone could give examples of a page handler they've used and/or made, would be
greatly appreciated.

my page system looks like this:

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
{
			cout << "*Your eyes open you see that your in some sort of box. A coffin.\n" 
			     << "Your hand starts burning. You look down in your coffin and see a burning light\n" 
			     << "A symbol burning into your hand. Some sort of rune*\n\n\n";

			do {

				cout << "Wait..i recognise this..ughh..my muscles ache...\n"
				     << "It feels like ive been ripped apart and put back together..\n"
				     << "atleast i the basics, how to walk, talk, and hopefully...fight\n\n"


				     << "hmm, theres tracks on the dust all over the floor. Someones been here recently.\n"
					 << "Well, theres no time to worry about that right now, the questions is...\n\n"


					 << "how do i get out of here? well it looks like im in some sort of hallway.\n"
					 << "at the end it looks like a torch is down there on the wall...\n\n"
					 
					 << "*your legs like jelly, like brand new limbs*\n\n"
					 
					 << "at the end of your hallway, under the light of the torch, you notice crate with some unlit torches\n"
					 << "you pick one up and lit it on the fire of the torch above torch.\n\n";
				system("pause");
						cout << "*In the hallway, you have 2 paths, but which way should i go?*\n"
							 << "left has this foul smell, while the right has this constant drip noise echoing, or you could go\n"
							 << "and check out your tomb?.\n\n"
							 << "which way do you choose? left(1) or right(2) or (3) to go back to your tomb\n\n";

				cin >> area_choice1;
				
			switch (area_choice1){


					case 1:
						system("cls");
						cout 	<< "(LEFT)"
								<< "going down the path the foul odour gets stronger and stronger until you get to a large room\n" 
								<< "full of empty coffins\n"
								<< "but these coffins weren't always empty? it seems like theyve been knocked out from the crypt\n" 
								<< "walls and smashed open...\n"
								<< "you walk coser to the closest coffin where the foul smell is strongest..inside lays the eaten remains\n" 
								<< "of some armoured soldier..\n"
							    << "but he wasnt the first one in hear..its too fresh a kill? maybe this was the one who left the tracks?\n\n"
								<< "*as you back away you hear this loud screech*\n\n"
								<< "you look towards where the noise came from and see two glowing blue eyes...\n"
								<< "and you recognise the eyes...ghoul!\n";
							cout << "GhoulHP: " << GhoulHP << "		playerHP: " << playerHP << "\n\n";
							cout << "type 'att' to defend yourself!";
							
						encounter_ghoul = true;
								cin >> attchoice;

									if (attchoice == "att")
									{
										cout << "\n" << "GhoulHp: " << GhoulHP << endl;
										cout << "you punch the ghoul and deal: -5HP" << endl;
												GhoulHP -=5;
										cout << "\n" << "GhoulHp: " << GhoulHP << "\n" << playerXP << "/\n" << requiredXP << endl;
										system("pause");
										encounter_ghoul = false;



											cout << "as you look around the room after defeating the Ghoul you notice a cicular objet..a wheel\n"
											     << "maybe itll be useful later\n\n";
											cout << "*you pick up the object*\n";
												bridge_wheel = true;

											cout << "press (1) to go back to hall, press (2) to stay in room and check around for anything\n";
											system("pause");




											cin >> direction;

										if(direction = 1)
											{
												break;
											}
										if(direction = 2)
										{
											cout << "you check around and notice a metal object\n"
											     << "*you go towards the metal object and realize what it is*\n\n"
											     << "it's a gauntlet...but not any gauntlet..it was mine...i remember it...\n"
											     << "it has blades along the side of it, arm-blades..still as sharp as the day they were forged.\n"
											     << "you also notice your boots right next to them.\n\n *you put them on*\n";	
											 gauntlets = true;
											 boots = true;
											 armour =+20;
											 arm_blade =+15;
										cout << "\n" << armour << "\n" << arm_blade << "\n";	 
										}
									}
						break;

					case 2:
						system("cls");
						cout << "(RIGHT)"
						     << "going down the right path you notice it leads to a giant circular room, in front of you\n"
							 << "a pool of water and it looks like a draw bridge on the other side. *you notice a pillar\n" 
							 << "with and axil coming out\n"
							 << "looks like its missing a wheel..maybe for the bridge?";

						cout << bridge_wheel;

								if(bridge_wheel = false)
									{
										cout << "I'll have to find the wheel for the bridge\n"
										     << "ill have to find the wheel\n";
									break;
									}

								if(bridge_wheel = true)
										{
											cout << "you place the wheel onto the axil and turn it, the bridge comes down.\n";

											bridge_down = true;

												if(bridge_down = true)
											{
												cout << "the bridge is now down do you wish to proceed?\n"
													 << "press (1) to leave, press (2) to stay and and look around at your tomb.\n";
													
													cin >> dungeonChoice;

													if(dungeonChoice = 1)
														{
															cout << "you walk across the bridge, and as you get closer to the end.\n" 
															     << "the wood gives way,\n"
															     << "from years of rotting and being constantly wet from the water.\n"
															     << "well that was close";
														exit_area =+1;
														break;	
														}
													if(dungeonChoice = 2)
														{
															cout << "*you go back to the path at your tomb entrance*\n\n";
														break;
														}
											}
										}
						break;


						case 3:
							system("cls");
						cout << "(TOMB)"
						     << "you search your tomb and notice chain...its your old chain mail\n"
						     << "you put it on and you feel how light it is...it ways almost nothing\n"
						     << "but can tell by how well its made it has to be strong.. a word slips from your mouth..mythril..\n"
						     << "going back you notice your old chest.\n\n *you put it on*\n";
						chain_mail = true;
						chest_plate = true;
						armour =+25;
						cout << "\n" << armour << endl;
					break;
					
					}

				}


with other parts defining the functions like armour and damage.
if anyone could shine some light on how i can make this neater and more reliable and efficient for finding errors i would greatly appreciate any feed back.
if(direction = 1)

= is assignment, == is comparison.

If you change the format in your editor so that tabs are replaced with with 3 or 4 spaces, then the code will display a lot better on this site. Here tabs are converted to 8 spaces, resulting in excessive indenting.

You need to make more use of functions. There is a bit of a rule that says that functions should be no longer than 40 lines, often much less than that. Each case of a switch should call a function.

I suspect you have global variables, which is bad .....
Also, try to avoid starting a new topic about the same subject.

http://www.cplusplus.com/forum/general/212710/
closed account (48T7M4Gy)
@OP Perhaps close off the other one by green ticking it as complete.

It might also be worthwhile considering a much more flexible and tidier way of storing and retrieving large amounts of text by using .txt files. So each slab of text could be on a page file. eg page1.txt, page2.txt etc

So when you get to a particular point in the game the instruction would be something like display("page1.txt"); where display(string filename) is the function to do that. The function is very simple. See http://www.cplusplus.com/doc/tutorial/files/ on reading text files.

This way of doing things also means editing what goes on each page fairly easy.

There are other (more complicated) alternatives eg just have one large file stored so you can load an array of messages at game start. But the principle is much the same.
Last edited on
thanks for this oppitunity ..............from joshua brown
theIdeasMan is there a way i can avoid global variables?
Topic archived. No new replies allowed.