Functionception

Hey guys, I'm currently making a Hangman program. And its going fairly well however...

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>


//--------------------------------
//------------Globals-------------
//--------------------------------
using namespace std;
const int MAX_TRIES = 7;
int letterFill(char, string, string&);
void gameboardskeleton(void);
//--------------------------------
//-----------ProtoTypes-----------
//--------------------------------
void PrintWelcome(void);
void gameboard1(void);
void gameboard2(void);
void gameboard3(void);
void gameboard4(void);
void gameboard5(void);
void gameboard6(void);
void gameboard7(void);


//--------------------------------
//--------------Main--------------
//--------------------------------
int main()
{
	string name;
	char letter;
	int num_of_wrong_guesses = 0;
	string word;

	srand(time(NULL));   

	
	// Ask user for for a category
	string category;
	cout << "\nChoose a Category!\n 1) Tv Characters\n 2) Animals\n 3) Video Games:" << endl;
	cin >> category;
	PrintWelcome();
	// compare category 
	if (category == "1") //TV Characters
	{
		//put all the string inside the array here
		string tvchars[] = { "Archer", "Bender", "Morty", "Rick", "Murderface", "Zoidberg", "Quagmire", "Meatwad", "Squanchy" };
		
		string word;

		int n = rand() % 9;
		word = tvchars[n];
		
		//call the function here for guessing game
		// Initialize the secret word with the - character.
		
		string unknown(word.length(), ' -'); //called unknown because I already had a name of secretword.
		
		// Loop until the guesses are used up*/
		gameboardskeleton();
		while (num_of_wrong_guesses < MAX_TRIES)
		{
			cout << "\n\n\t\t\t" << unknown;
			cout << "\n\nGuess a letter: \n"; 
			cout << word << " is the word you're looking for\n"; //used for debug
			cin >> letter;
			// Fill secret word with letter if the guess is correct,
			// otherwise increment the number of wrong guesses.
	
			if (letterFill(letter, word, unknown) == 0)
			{
				cout << endl << "Try again." << endl;cout << "The word was : " << word << endl;
				num_of_wrong_guesses++;
				
			}
			if (num_of_wrong_guesses == 0)
			{
				gameboardskeleton();
			}
			if (num_of_wrong_guesses == 1)
			{
				gameboard1();
			}
			if (num_of_wrong_guesses == 2)
			{
				gameboard2();
			}
			if (num_of_wrong_guesses == 3)
			{
				gameboard3();
			}
			if (num_of_wrong_guesses == 4)
			{
				gameboard4();
			}
			if (num_of_wrong_guesses == 5)
			{
				gameboard5();
			}
			if (num_of_wrong_guesses == 6)
			{
				gameboard6();
			}
			if (num_of_wrong_guesses == 7)
			{
				gameboard7();
			}
			else
			{
				cout << endl << "You found a letter." << endl; //lettertrue function
			}
			
		

			// Tell user how many guesses has left.
			cout << "You have " << MAX_TRIES - num_of_wrong_guesses << " guesses left." << endl;
			// Check if user guessed the word.
			if (word == unknown)
			{
				cout << word << endl;
				cout << "you win"; //youwin congrats function
				break;
			}
		}
		if (num_of_wrong_guesses == MAX_TRIES)
		{
			cout << "\nSorry, you lose...you've been hanged." << endl; //youlose function
			cout << "The word was : " << word << endl; //tbdeleted 
		}
		cin.ignore();
		cin.get();
		return 0;
	}

	
		return 0;
	}

}

//--------------------------------
//---------Implementation---------
//--------------------------------

int letterFill(char guess, string secretword, string &guessword)
{
	int i;
	int matches = 0;
	int len = secretword.length();
	for (i = 0; i< len; i++)
	{
		// Did we already match this letter in a previous guess?
		if (guess == guessword[i])
			return 0;
		// Is the guess in the secret word?
		if (guess == secretword[i])
		{
			guessword[i] = guess;
			matches++;
		}
	}
	return matches;
}

void PrintWelcome(void)
{
	cout << " ________________________________________________________________\n";
	cout << "|          Each letter is represented by a dash.                 |\n";
	cout << "|         You have to type only one letter in one try.           |\n";
	cout << "|        You have " << MAX_TRIES << " tries to try and guess the word.          |\n";
	cout << "|________________________________________________________________|\n";
}

void gameboardskeleton(void)
{
	cout << "\t       ____________		    " << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t        ___________|_________" << endl;
}
void gameboard1(void)
{
	cout << "\t       ____________		    " << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t     ( )           |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t        ___________|_________" << endl;
}
void gameboard2(void)
{
	cout << "\t       ____________		    " << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t     ( )           |			" << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t        ___________|_________" << endl;
}
void gameboard3(void)
{
	cout << "\t       ____________		    " << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t     ( )           |			" << endl;
	cout << "\t      |_           |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t        ___________|_________" << endl;
}
void gameboard4(void)
{
	cout << "\t       ____________		    " << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t     ( )           |			" << endl;
	cout << "\t     _|_           |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t        ___________|_________" << endl;
}
void gameboard5(void)
{
	cout << "\t       ____________		    " << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t     ( )           |			" << endl;
	cout << "\t     _|_           |			" << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t        ___________|_________" << endl;
}
void gameboard6(void)
{
	cout << "\t       ____________		    " << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t     ( )           |			" << endl;
	cout << "\t     _|_           |			" << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t       \\           |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t        ___________|_________" << endl;
}
void gameboard7(void)
{
	cout << "\t       ____________		    " << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t     ( )           |			" << endl;
	cout << "\t     _|_   R.I.P.  |			" << endl;
	cout << "\t      |            |			" << endl;
	cout << "\t     / \\           |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t                   |			" << endl;
	cout << "\t        ___________|_________" << endl;
}


I want to make a function called gameboardupdate that includes all the gameboard checks.
1
2
3
(if (num_of_wrong_guesses == 1)
			{
				gameboard1();)


the problem I'm running into is that when I do, the program doesn't recognize int num_of_wrong_guesses and if I just re-declare it at the beginning of my function, everytime it checks it would just reset the number of wrong guesses to 0. Is there a way I can just call the integer? Or is there a better way I can do this?

Thanks in advance.
Why not just pass it into the function as an argument?
I don't believe we've covered that in class yet. Do you have any articles you could point me to that would help me understand what you just said? I ran a google search and not much help arose.
I don't believe we've covered that in class yet.

Then how come you're already passing arguments into functions in your code?
I don't believe I know what you mean by "passing arguments". As for your previous post; Did you mean to use things like pointers?
At line 147, you define a function that takes 3 arguments.

At line 72, you call that function, passing in 3 arguments.

And, no, I didn't mean to say anything about pointers.

My professor gave me a hand with that before break. I didn't have time to ask him about what it meant or anything like that. Which was my plan after break. However, it looks like I can just move line 34 to my globals section and everything works out with making that function. Since num_of_wrong_guesses was only defined inside of main, it didn't know what it was outside of it. Thanks for your input MikeyBoy.
However, it looks like I can just move line 34 to my globals section and everything works out with making that function.

You could do that, although global variables are generally speaking not a good idea, and I wouldn't advise getting into the habit of using them.

The chapter on functions in your textbook will explain function arguments, if your professor didn't. Alternatively, you could look at the tutorial here:

http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.