Hang Man Game

Hi! Could someone help me find the logic error in my functions? I don't think its the boolean, but I could be wrong. I have fiddled with the other quite a bit, especially on determining the length of the array, but can't seem to correct the error.

It is supposed to function like a hangman game, first creating a template of underscores with the same number of characters as the secret word, editing that template based on guesses, and checking to see if the template matched the secret word.

Thank you!!

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
  // CS 200   
// "Hangman" word guessing game.
//   Program shell hangMan.cpp

#include<iostream>

using namespace::std;


// Function prototypes
void createTemplate(const char secretWord[], char guessTemplate[]);

void updateTemplate(const char secretWord[], char guessLetter,
	char guessTemplate[]);

bool matchTemplate(const char secretWord[], const char guessTemplate[]);

//--------------------------------------------------------------------

const int MAX_SIZE = 15;
int main()
{
	const int NUM_LINES = 25;        // Number of lines on the screen

	char secretWord[MAX_SIZE],      // Secret word to be guessed
		guessTemplate[MAX_SIZE];   // Template showing correct guesses
	char guessLetter;               // Letter guessed

	int numGuesses,                 // Number of letters guessed
		j;                          // Counter

									// Get the secret word.
	cout << endl << "Enter the secret word: ";
	cin >> secretWord;

	// Scroll it off the screen.
	for (j = 0; j < NUM_LINES; j++)
		cout << endl;

	// Create an empty guess template.
	createTemplate(secretWord, guessTemplate);

	// Play the game.
	numGuesses = 0;
	while (!matchTemplate(secretWord, guessTemplate))
	{
		numGuesses++;
		cout << guessTemplate << endl;

		// Get a letter guess.
		cout << "Guess a letter: ";
		cin >> guessLetter;

		// Fill in the occurrences of this letter.
		updateTemplate(secretWord, guessLetter, guessTemplate);
	}

	// Display the secret word and the number of guesses.
	cout << guessTemplate << "=" << secretWord << endl;
	cout << "You guessed the word in " << numGuesses << " guesses"
		<< endl;

	return 0;
}


//--------------------------------------------------------------------
// Insert your function implementations here.
//--------------------------------------------------------------------
void createTemplate(const char secretWord[], char guessTemplate[])
{
	//   The guessTemplate will be an array of characters, all cells
	//          filled with the char '-' and with a '\0' at the end
	//   The number of cells filled will be determined by the numbers
	//          of characters in secretWord
	//   If secretWord is the C++ string  monday
	//      Then guessTemplate will be    ------

	//   ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION

	//   NOTE: An array of char is NOT a C++ string until the '\0' character
	//         has been added after the last character.  You will need
	//         to add the '\0' character to the guessTemplate array in order
	//         for that array to become a C++ string

	int length = strlen(guessTemplate);

	for (int i= 0; i < (length-1); i++)
	{
		guessTemplate[i] = '_';
	}

	cout << guessTemplate[];



}


void updateTemplate(const char secretWord[], char guessLetter,
	char guessTemplate[])
{
	//   Update guessTemplate by replacing '-' in the array with
	//      guessLetter for each corresponding cell in secretWord
	//   If secretWord is kansas and guessLetter is 's'.
	//   If guessTemplate was --n--- then guessTemplate would
	//   be changed to --ns-s


	//   ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION

	int length = strlen(guessTemplate);
	for (int i = 0; i < length; i++)
	{
		if (guessLetter = secretWord[i])
			guessTemplate[i] = guessLetter;
	}







}


bool matchTemplate(const char secretWord[], const char guessTemplate[])
{

	//  will return true if secretWord and guessTemplate contain the same
	//  C++ string and will return false otherwise

	//   ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION

	
	if (secretWord[] == guessTemplate[])
	{
		return true;
	}
	else
	{
		return false;
	}






}
This will not work. if (secretWord[] == guessTemplate[])
You need to use strcmp to compare character arrays.

The comparison operator is == if (guessLetter = secretWord[i])

Don't forget this.
You will need
// to add the '\0' character to the guessTemplate array in order
// for that array to become a C++ string
Topic archived. No new replies allowed.