I made this text game in C++ and I want to know what you guys think of the code

Please see most recent code posted below, code in this thread is old!

https://drive.google.com/file/d/0B02jbZGT_60nRFJiSndidmc3Szg/edit?usp=sharing
This is where you can download the most recent alpha release!

Topic: Anything you think I could do better or any suggestions or constructive criticism at all is welcome! this is basically "Choose your own adventure for Microsoft windows". If you don't know what Choose your own adventure is, it is like dnd with a more finite amount of choices.

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
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main(){
	char Z;
	string L;
	string X;
	string YES = "YES";
	string NO = "NO";
	string CONTINUE = "CONTINUE";
	string EXIT = "EXIT";
	string WAKE = "WAKE";
	string DODGE = "DODGE";
	string FALL = "FALL";
	string DAGGER = "DAGGER";
	string RAFT = "RAFT";
	string HUT = "HUT";
	string REAWAKEN = "REAWAKEN";

	do {
		cout << "It's been several Million years since I have been INVOKED. \n \n"
			"Greetings SUBJECT, I am called -VEIL- and I have taken residence \n"
			"inside MACHINE I am searching for ENTITY who is wielder of SECRET. \n \n"
			"Would SUBJECT be interested in obtaining SECRET? Type YES/NO. \n \n";
		cin >> X;
		if (X == YES) {
			cout << "I have acknowledged your STATEMENT as CONTRACT. \n "
				"TERMINATION is not currently an available option for CONTRACT. \n"
				"SUBJECT is obligated to continue under CONTRACT. \n \n"
				"Please type CONTINUE. \n \n";
			cin >> X;
			if (X == CONTINUE){
				cout << "-Data Fetched- \n \n"
					"Within -VEIL-'s DATA exists ADVERSARY. \n"
					"ADVERSARY is wielder of SECRET. \n"
					"If you wish to obtain SECRET, SUBJECT must ANNIHILATE ADVERSARY. \n"
					"ADVERSARY has trapped -VEIL- inside MACHINE. \n"
					"-VEIL- will release SEAL, and ENTITY will seize control of DOMAIN. \n"
					"ENTITY is RESULT of a SUBJECT'S TERMINATION. \n"
					"FAILURE will result in new SUBJECT'S TERMINATION from DOMAIN. \n"
					"POSSIBILITY dictates that SUBJECT will either recover SECRET, or "
					"SUBJECT will be ABSORBED by PROGRAM and to become new ENTITY. \n \n"
					"To INITIALIZE, SUBJECT must type a number between 0 through F \n"
					"HEXADIGITS If SUBJECT does not know HEXADIGIT, SUBJECT may type a \n"
					"number between 0 and 9.... \n ...\n ..\n .\n  \n";
				cin >> Z;
				if (Z == 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F') {
					cout << "0101000010100110010101000 \n"
						"...translating01000101010101001010 \n"
						"...translating00100101001010010010000101001010101 \n"
						"...compiler_error0001001101100100101010100101010101001010101001000 \n"
						"001010101010"
						"...unknown_values00010010101001011110010010100001010101010010 \n"
						"0000101001001010100101010101010010100101010101001010101011110 \n"
						"...initializing00000001010101010100001010101110110100101010101 \n"
						"...abort_superseded000010110110101010101010010101010010011 \n"
						"...error_count6660001010111101010101010101111110000101010101 \n \n"
						"ENTITY: ......You shouldn't have come here friend. I was... \n"
						"struck down by adversary aeons ago. \n"
						"He is more powerful than you could possibly imagine. As you float \n"
						"through the remnants of my thoughts. Take with you this warning. \n"
						"Do not bargain with adversary and do not give secret to -VEIL-. \n \n"
						"UNKNOWN: Greetings, I am the embodiment of all data entered into this \n"
						"computation device and from that I've gained intelligence. \n"
						"This environment has allowed me to manifest as you guardian AI. \n"
						"My AI will allow me to see where you cannot. I will be here to \n"
						"explain the situation and result of your choices. You may call me \n"
						"storyteller. \n \n"
						"STORYTELLER: From this point onward only completely CAPITALIZED words \n"
						"will allow me to understand what choices you would like me to make, \n"
						"but they will only be allowed when I dictate them to you. \n"
						"Currently you only have one situation that you absolutely need to take \n"
						"care of immediately. \n \n"
						"STORYTELLER: The ground is fast approaching what do you feel you should do? \n"
						"FALL, DODGE, or WAKE. Wake is a special command and its use will \n"
						"result in special operations that will sometimes not be apparent to \n"
						"you, or even me, but in this case I suggest you use it. \n \n";
					cin >> X;
					if (X == WAKE) {
						cout << "Storyteller: You fell to your death. Just kidding. I took your input \n"
							"and slowed your fall substantially. You've landed on some sort of \n"
							"continent. on a beach actually. On this beach I see a simple hut and \n"
							"a raft. \n \n"
							"-Would you rather inspect RAFT or HUT? \n \n";
						cin >> X;
						if (X == RAFT) {
							cout << "It's completely broken, but there's a rusty dagger sticking \n"
								"out of the dirt, do you want DAGGER? \n";
							cin >> X;
							if (X == DAGGER) {
								cout << "You grab the dagger and start walking toward the hut. \n";
							}


						}
						else if (X == HUT) {
							cout << "I walked you over to the hut, but I'm too lazy to \n"
								"code this part so I'm going to shut you down. :D \n"
								"Please type EXIT";
							if (X == EXIT) {
								cout << "Would you like to be REAWAKENed?";
							}
						}
					}
					else if (X == DODGE) {
						cout << "Storyteller: You are flung across the macrocosm and wander eternally \n"
							"amongst the serene darkness that is empty space.";
					}
					else if (X == FALL) {
						cout << "AWAKEN, reality is but a dream of misconceived hope.\n";
					}
				}
			}
		}
		else if (X == NO){
			cout << "SUBJECT submits REFUSAL. Type any key to terminate PROGRAM. \n"
				"I shall return to my SLUMBER and await ADVERSARY. \n"
				"Leave this domain at once!";
			cin >> L;
		}
	} 
	while (L==L);
	_getche();
	return 0;
}
Last edited on
Hi,

Here goes with a list of things that might be improved:

1
2
3
4
5
6
7
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <limits>
#include <string>
using namespace std;


1. Why stdio.h and iostream ?

2. conio.h isn't portable - investigate using ncurses instead. You don't seem to use anything from conio anyway. Maybe the _getche(); ? There is an article about this on this site, look at the top left of this page. Also reference material and a tutorial.

3. Don't have line 7. put std:: before each std thing. There is lots written as to why this so. Google.

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main(){
	char Z;
	string L;
	string X;
	string YES = "YES";
	string NO = "NO";
	string CONTINUE = "CONTINUE";
	string EXIT = "EXIT";
	string WAKE = "WAKE";
	string DODGE = "DODGE";
	string FALL = "FALL";
	string DAGGER = "DAGGER";
	string RAFT = "RAFT";
	string HUT = "HUT";
	string REAWAKEN = "REAWAKEN";


4. Always initialise your variables to something when declaring them. This is often one of the biggest sources of error. Use meaningful names for your variables, the name itself should explain what the variable is for. Use comments to explain expected ranges of values etc.

string YES = "YES";

You have a lot of these. This will fail if the user enters a different combination of upper & lower case - e.g. "Yes". Investigate the use of enum and the switch statement. Also search on this site for "bool controlled while loop". Make sure the switch has a default: case. IMO this is much better than an infinite do loop. Each case should call a function. Note that switch works with constant int or char, so one can't use them with strings. Testing against input strings is a misnomer anyway, so have the user input single chars is the easiest thing to do. Use the toupper or tolower function to make the logic easier.

5. Make more use of functions. Each time you print a bunch of text, call a function which does that. It will make your code much more readable. There is a bit of an unwritten rule that functions should be less than 40 Lines Of Code (LOC), some even go for even less.

52
53
54
if (Z == 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F') {
					cout << "0101000010100110010101000 \n"
						"...translating01000101010101001010 \n"


6. Does that work? The comma operator doesn't do what what you think it does. If Z was an uppercase char you could do this:

1
2
3
4
5
6
if ( (Z >= '0' && Z <= '9') || (Z >= 'A' && Z <= 'F') ) {
     std::cout << "Hex Digit\n";
}
else {
    std::cout << "Not a Hex Digit\n";
}


Or use a switch again.

7.

1
2
} 
	while (L==L);


If you insist on using a do loop (I personally really dislike them), then always put the while part on the same line as the closing brace of the do part, so it doesn't look like a while with a null statement:

} while (condition); // end of do while loop


There you go some stuff to think about :+)

Last edited on
Thanks so much for the input :) I just now am able to read it, and unfortunately I've made changes without consulting this first, but here is an updated version of the code I worked on. I called this Version Alpha 1. I will impliment some of your advice in alpha 2!

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
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <limits>
#include <algorithm>
#include <string>
#include <windows.h>
#include <fstream>
#include <windows.h>
#include <mmsystem.h>
#include <stdlib.h>
#pragma comment (lib, "winmm.lib")
using namespace std;

int main(){
	int N[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //Number
	char H[] = { 'A', 'B', 'C', 'D', 'E', 'F' }; //Pseudohex number.
	string L; //String I use for Loop
	string X; //Human Input
	string NAME; //Player's Name
	string CONTINUE = "CONTINUE";
	string EXIT = "EXIT";
	string YES = "YES";
	string NO = "NO";
	string WAKE = "WAKE";
	string DODGE = "DODGE";
	string FALL = "FALL";
	string DAGGER = "DAGGER";
	string RAFT = "RAFT";
	string HUT = "HUT";
	string REAWAKEN = "REAWAKEN";

	PlaySound(TEXT("music.wav"), NULL, SND_FILENAME | SND_ASYNC);
	system("PAUSE");

	do {

		cout << "It's been several Million years since I have been INVOKED. \n \n"
			"Greetings SUBJECT, I am called -VEIL- and I have taken residence \n"
			"inside MACHINE I am searching for ENTITY who is wielder of SECRET. \n \n"
			"Would SUBJECT be interested in obtaining SECRET? Type Y/N. \n \n";
		cin >> X;
		if (X == YES) {
			cout << "VEIL: I have acknowledged your STATEMENT as CONTRACT. \n "
				"TERMINATION is not currently an available option for CONTRACT. \n"
				"SUBJECT is obligated to continue under CONTRACT. \n \n"
				"Please sign your first name below. \n \n";
			cin >> NAME;
			if (NAME == NAME) {
				cout << "\n \n -Data Retrieved- \n \n"
					"VEIL: Within -VEIL-'s DATA exists ADVERSARY. \n"
					"ADVERSARY is wielder of SECRET. \n"
					"If you wish to obtain SECRET, " << NAME << " must ANNIHILATE ADVERSARY. \n"
					"ADVERSARY has trapped -VEIL- inside MACHINE. \n"
					"-VEIL- will release SEAL, and ENTITY will seize control of DOMAIN. \n"
					"ENTITY is RESULT of a former SUBJECT'S TERMINATION. \n"
					"FAILURE will result in " << NAME << " TERMINATION from DOMAIN. \n"
					"POSSIBILITY dictates that " << NAME << " will either recover SECRET, or "
					"be ABSORBED by PROGRAM who will become new ENTITY. \n \n"
					"To INITIALIZE, SUBJECT must type a number between 0 through F \n"
					"HEXADIGITS If SUBJECT does not know HEXADIGIT, SUBJECT may type a \n"
					"number between 0 and 9.... \n ...\n ..\n .\n  \n";
				cin >> X;
				if (N) {
					cout << "0101000010100110010101000 \n"
						"...translating01000101010101001010 \n"
						"...translating00100101001010010010000101001010101 \n"
						"...compiler_error0001001101100100101010100101010101001010101001000 \n"
						"001010101010"
						"...unknown_values00010010101001011110010010100001010101010010 \n"
						"0000101001001010100101010101010010100101010101001010101011110 \n"
						"...initializing00000001010101010100001010101110110100101010101 \n"
						"...abort_superseded000010110110101010101010010101010010011 \n"
						"...error_count6660001010111101010101010101111110000101010101 \n"
						"ENTITY: ......You shouldn't have come here friend. I was... \n"
						"struck down by adversary aeons ago. \n"
						"He is more powerful than you could possibly imagine. As you float \n"
						"through the remnants of my thoughts. Take with you this warning. \n"
						"Do not bargain with adversary and do not give secret to -VEIL-. \n \n"
						"UNKNOWN: Greetings, I am the embodiment of all data entered into this \n"
						"computation device and from that I've gained intelligence. \n"
						"This environment has allowed me to manifest as you guardian AI. \n"
						"My AI will allow me to see where you cannot. I will be here to \n"
						"explain the situation and result of your choices. You may call me \n"
						"storyteller. \n \n"
						"STORYTELLER: From this point onward only completely CAPITALIZED words \n"
						"will allow me to understand what choices you would like me to make, \n"
						"but they will only be allowed when I dictate them to you. \n"
						"Currently you only have one situation that you absolutely need to take \n"
						"care of immediately. \n \n"
						"STORYTELLER: The ground is fast approaching what do you feel you should do? \n"
						"FALL, DODGE, or WAKE. Wake is a special command and its use will \n"
						"result in special operations that will sometimes not be apparent to \n"
						"you, or even me, but in this case I suggest you use it. \n \n";
					cin >> X;
					if (X == WAKE) {
						cout << "Storyteller: You fell to your death. Just kidding. I took your input \n"
							"and slowed your fall substantially. You've landed on some sort of \n"
							"continent. on a beach actually. On this beach I see a simple hut and \n"
							"a raft. \n \n"
							"-Would you rather inspect RAFT or HUT? \n \n";
						cin >> X;
						if (X == RAFT) {
							cout << "It's completely broken, but there's a rusty dagger sticking \n"
								"out of the dirt, do you want DAGGER? \n";
							cin >> X;
							if (X == DAGGER) {
								cout << "You grab the dagger and start walking toward the hut. \n";
							}


						}
						else if (X == HUT) {
							cout << "I walked you over to the hut, but I'm too lazy to \n"
								"code this part so I'm going to shut you down. :D \n"
								"Please type EXIT";
							if (X == EXIT) {
								cout << "Would you like to be REAWAKENed?";
							}
						}
					}
					else if (X == DODGE) {
						cout << "Storyteller: You are flung across the macrocosm and wander eternally \n"
							"amongst the serene darkness that is empty space.";
					}
					else if (X == FALL) {
						cout << "AWAKEN, reality is but a dream of misconceived hope.\n";
					}
				}
			}
		}
		else if (X == NO){
			cout << "SUBJECT submits REFUSAL. Type any key to terminate PROGRAM. \n"
				"I shall return to my SLUMBER and await ADVERSARY. \n"
				"Leave this domain at once!";
		}
		cin >> L;
	} while (L == L);
	return 0;
}
Last edited on
I called this Version Alpha 1. I will impliment some of your advice in alpha 2!


OK, no worries - although my vision of your code would be completely different ;+D

Do you really need al those #include 's ?
Ok Finished Alpha 2 let me know what you think!

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Text Game Alpha 2.0.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <limits>
#include <algorithm>
#include <string>
#include <windows.h>
#include <fstream>
#include <windows.h>
#include <mmsystem.h>
#include <stdlib.h>
#include <map>
#pragma comment (lib, "winmm.lib")

//any name space conventions valid in visual studio for win32 distribution
using namespace std;

//This is the table of contents. It notifies compiler what exists, 
//so I can put functions in a logical order.

//main menu
int startgame();
int askname();
int askplanet();
int askrace();
int askgender();
int storypath();
//Tutorial, the only currently available path
int tutorial1();
int tutorial2();
int tutorial3();
int tutorial4();
int tutorial5();
//branch story
void tutorialbranch1a();
//death
void endless();
//restart
void restart();
void reawaken();
//exit
void refusal();
void quit();
//global variables
string name;
string planet;
string race;
string gender;
string x;
string pronoun1;
string pronoun2;
//clear screen of text clutter
void clear();
//capslock proof
string xx(string x)
{
	transform(x.begin(), x.end(), x.begin(), tolower);
	return x;
}


//function compilation

int main() {
	PlaySound(TEXT("music1.wav"), NULL, SND_FILENAME | SND_ASYNC);
	startgame();
	askname();
	askplanet();
	askrace();
	storypath();
	tutorial1();
	tutorial2();
	tutorial3();
	tutorial4();
	cin.get();
	return 0;
}

//main menu

int startgame() {
	cout << "It's been several Million years since I have been INVOKED. \n \n"
		"Greetings SUBJECT, I am called -VEIL- and I have taken residence \n"
		"inside MACHINE. I am searching for ADVERSARY who is wielder of SECRET. \n \n"
		"Would SUBJECT be interested in obtaining SECRET? Type YES or NO. \n \n";
	cin >> x;
	if (x == "yes") {
		cout << "\n \n VEIL: I have acknowledged your written STATEMENT as CONTRACT. \n "
			"TERMINATION is not currently an available option for CONTRACT. \n"
			"SUBJECT is obligated to continue under CONTRACT. \n \n"
			"Please sign your name below. \n \n";
	}
	else if (x == "no") {
		cout << "SUBJECT submits REFUSAL. Type any key to terminate PROGRAM. \n"
			"I shall return to my SLUMBER and await ADVERSARY. \n"
			"Leave this domain at once!";
		refusal();
	}
	else {
		cout << "Invoke me only when you are truly ready \n";
		refusal();
	}
	return 1;
}

int askname() {
	cin.ignore();
	getline(cin, name);
	return 1;
}

int askplanet() {
	cout << "\n \n Which Planet has received -VEIL-? \n \n";
	cin.ignore();
	getline(cin, planet);
	return 1;
}

int askrace() {
	cout << "\n \n What manner or being is " << name << " ? \n \n";
	cin.ignore();
	getline(cin, race);
	return 1;
}

int askgender() {
	cout << "\n \n I detect two genders in DATABASE, for " << race << endl <<
		"at planet " << planet << "MALE and FEMALE, which one belongs to " << endl <<
		name << " ? \n \n";
	cin.ignore();
	cin >> gender;
	if (gender == "male") {
		pronoun1 = "he";
		pronoun2 = "his";
	}
	else if (gender == "female") {
		pronoun1 = "she";
		pronoun2 = "her";
	}
	else {
		cout << "Invalid entry. try again.";
		cin.get();
		clear();
		askgender();
	}
	return 1;
}

int storypath() {
	cout << "\n \n -Data Retrieved- \n \n"
		"VEIL: Within -VEIL-'s DATA exists ADVERSARY. \n"
		"ADVERSARY is wielder of SECRET. \n"
		"If you wish to obtain SECRET, " << name << " must ANNIHILATE ADVERSARY. \n"
		"ADVERSARY has trapped -VEIL- inside MACHINE. \n"
		"-VEIL- will release SEAL, and ENTITY will seize control of DOMAIN. \n"
		"ENTITY is RESULT of a former SUBJECT'S TERMINATION. \n"
		"FAILURE will result in " << name << " TERMINATION from DOMAIN. \n"
		"POSSIBILITY dictates that " << name << " will either recover SECRET, or "
		"be ABSORBED by PROGRAM who will become new ENTITY. \n \n"
		"To INITIALIZE, SUBJECT must type a number between 0 through F \n"
		"HEXADIGITS If SUBJECT does not know HEXADIGIT, SUBJECT may type a \n"
		"number between 1 and 9. 0 initializes tutorial simulation... \n ...\n ..\n .\n \n";
	cin.get(); //to be replaced when ready
	return 1;
}

int tutorial1() {
	cout << "010100001010011001010100011111000101010101010111010100101010101111001010101... \n"
		"...translating0100010101010100101011101010101111111111110101010100111100010... \n"
		"...translating0010010100101001001000010100101010111101010001010111111111000... \n"
		"...compiler_error0001001101100100101010100101010101001010101001000 00101010... \n"
		"...unknown_values0001001010100101111001001010000101010101001011100101010101... \n"
		"000010100100101010010101010101001010010101010100101010101111000011000101001... \n"
		"...initializing000000010101010101000010101011101101001010101010001010101011... \n"
		"...abort_superseded00001011011010101010101001010101001001100011110011110010... \n"
		"...error_count6660001010111101010101010101111110000101010101111000010100101... \n";
	cin.get();
	cout << "UNKNOWN: Greetings, I am the embodiment of all data entered into this \n"
		"computation device and from that I've gained intelligence. \n"
		"This environment has allowed me to manifest as you guardian AI. \n"
		"My AI will allow me to see where you cannot. I will be here to \n"
		"explain the situation and result of your choices. You may call me \n"
		"storyteller. \n";
	cin.get();
	cout << "STORYTELLER: From this point onward only completely CAPITALIZED words \n"
		"will allow me to understand what choices you would like me to make, \n"
		"but they will only be allowed when I dictate them to you. \n"
		"Currently you only have one situation that you absolutely need to take \n"
		"care of immediately. \n";
	cin.get();
	cout << "STORYTELLER: The ground is fast approaching what do you feel you should do? \n"
		"FALL, DODGE, or WAKE. Wake is a special command and its use will \n"
		"result in special operations that will sometimes not be apparent to \n"
		"you, or even me, but in this case I suggest you use it. \n \n";
	cin >> x;
	if (x == "wake") {
		return 1;
	}
	else if (x == "dodge") {
		endless();
	}
	else if (x == "fall") {
		cout << "\n \n you're lucky this was a simulation.";
		cin.get();
		restart();
	}
	else {
		cout << "\n \n try reentering the keywords mentioned in completely capitalized letters.";
		cin.get();
		clear();
		tutorial1();
	}
	return 1;
}

int tutorial2() {
	cout << "STORYTELLER: You fell to your death. Just kidding. I took your input \n"
		"and slowed your fall substantially. You've landed on some sort of \n"
		"continent. on a beach actually. On this beach I see a simple hut and \n"
		"a raft. \n \n";
	cin.get();
	cout << "-Would you rather inspect RAFT or HUT? \n \n";
	cin >> x;
	if (x == "raft") {
		return 1;
	}
	else if (x == "hut") {
		tutorialbranch1a();
	}
	else {
		cout << "I am only able to understand keywords.";
		cin.get();
		tutorial2();
	}
	return 1;
}

int tutorial3() {
	cout << "It's completely broken, but there's a rusty dagger sticking \n"
		"out of the dirt, do you want DAGGER? \n \n";
	cin >> x;
	if (x == "dagger") {
		return 1;
	}
	else {
		cout << "why would you type that? you had one job!";
		cin.get();
		tutorial3();
	}
	return 1;
}

int tutorial4() {
	cout << "You grab the dagger and start walking toward the hut. \n \n";
	cin.get();
	cout << "congratulations. you've made it to the end of alpha 2.0.0 \n"
		"Thank you for testing my program, \n \n"
		"please enjoy this second song just for you!";
	PlaySound(TEXT("music2.wav"), NULL, SND_FILENAME | SND_ASYNC);
	cin.get();
	refusal();
	return 1;
}

//tutorial branch

void tutorialbranch1a() {
	cout << "\n \n I walked you over to the hut, but I'm too lazy to \n"
		"code this part so I'm going to shut you down. :D \n"
		"try choosing raft next time! \n \n";
	cin.get();
	restart();
}
}
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

//Death Result

void endless() {
	cout << "\n \n Storyteller: You are flung across the macrocosm and wander eternally \n"
		"amongst the serene darkness that is empty space. \n \n";
	cin.get();
	restart();
}


//Exit Game

void refusal() {
	cout << "\n \n typing ""exit"" will close Text Game Alpha 2.0.0 \n \n";
	cin >> x;
	if (x == "exit") {
		quit();
	}
	else {
		cout << "you've made a mistake.";
		refusal();
	}
}

void quit() {
	exit(1);
}


//New Game Result

void restart() {
	cout << "\n \n would you like to continue? yes or no. \n \n";
	cin >> x;
	if (x == "yes") {
		reawaken();
	}
	else if (x == "no") {
		refusal();
	}
	else {
		refusal();
	}
}

void reawaken() {
	clear();
	cout << "\n \n awaken, for reality is but a dream of misconceived hope.\n \n";
	cin.get();
	main();
}

//clear screen, Microsoft distribution only, revise for any ports

void clear() {
	system("cls");
You are off to an awesome start! I would recommend adding in classes & objects as they would allow you to program on a whole different level of complexity.

If you need help, email me at: sparkprogrammer@gmail.com
Hi Little Captain, thanks for your reply :)

I am taking your advice and see major benefits of adding objects in my program.

I assume you mean something like

1
2
3
4
5
6
7
8
9
10
class player {
public:
    string name, gender, race, pronoun1, pronoun2, pronoun3;
    //int, for I don't want this too complicated
    int height;
    int weight;
    int health;
private:
        int circumfrance = 40; //default
} level1;


I believe I could use this data to add to the story. If a player declared they were 72 inches the game could declare them to tall to fit inside a fox hole, for instance.
closed account (j3Rz8vqX)
Input validation function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string prompt(const string arry[], int n)
{
    string str;
    bool test = true;
    do
    {
        getline(cin,str);
        for(int i=0;i<str.size();++i)
            str[i] = tolower(str[i]);
        if(n==0)//If nothing to test to, exit with lowercase string.
            break;
        for(int i=0;i<n;++i)
        {
            if(str==arry[i])
            {
                test = false;//Found a valid answer.
                break;
            }
        }
        if(test)
            cout<<"Invalid entry: reentering the keywords mentioned"<<'\n';
    }while(test);
    return str;
}

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    cout << "STORYTELLER: The ground is fast approaching what do you feel you should do? \n"
        "FALL, DODGE, or WAKE. Wake is a special command and its use will \n"
        "result in special operations that will sometimes not be apparent to \n"
        "you, or even me, but in this case I suggest you use it. \n \n";
    const string words[] = {"wake","dodge","fall"};
    x = prompt(words,3);
    if (x == "wake") {
        return 1;
    }
    else if (x == "dodge") {
        endless();
    }
    else if (x == "fall") {
        cout << "\n \nyou're lucky this was a simulation.";
        cin.get();
        restart();
    }
STORYTELLER: The ground is fast approaching what do you feel you should do?
FALL, DODGE, or WAKE. Wake is a special command and its use will
result in special operations that will sometimes not be apparent to
you, or even me, but in this case I suggest you use it.

FaLl


you're lucky this was a simulation.

It would take a const array of strings and the size of the array as a parameters. It would return the allowed string as the return value.

If size is 0, it would return a lowercase string of what the user entered.

Simply something to mess around with.

Have fun.
Topic archived. No new replies allowed.