Hangman Program with String Arrays and Headers

I am trying to complete a simple Hangman game program which uses a Header that declares the two functions showGallows and showSolved.

The main problem I have is what to do with the function showSolved. As the comment has shown, I want the function showSolved to do two things first:

1. Store the properly guessed letters in an array
2. Show the user which letters have been correctly guessed.

I feel that I can figure out the main() part of this program if I can get a better understanding of what I should do with showSolved.

Please, spare some time to help me understand how I should proceed with showSolved.

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
 // Hangman_Assignment_Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "hangman.h"

using namespace std;


int main()
{

	//defines 3 'C-style' strings
	//One for the corretly guessed letters
	//The second, the incorrectly guessed letters
	//the last, the word to be guessed

	char wordToBeGuessed[] = "rainbow";
	char correctlyGuessedLetters[10] = "#######";
	char incorrectlyGuessedLetters[10];
	int hang = 0;
	int win_point = 0;

	//introduction to the game

	cout << "Welcome to my Hangman Game!" << endl;

	//calls showGallows() and showSolved()

	do
	{
		char theGuess[10] = "";
		showGallows(hang);
		cout << endl;
		cout << "Time to make your guess (NO HINTS!):";
		cin >> theGuess;
		showSolved(wordToBeGuessed, theGuess);

		
	} while (win_point != 0);

}

//using the main .cpp file to define the functions of the hangman.h


//definition of showGallows
void showGallows(int guessLimit)
{

	//displays the gallows
	if (guessLimit == 0)
	{
		cout << "_________________" << endl;
		cout << " |        |      " << endl;
		cout << " |        |      " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;

	}

	if (guessLimit == 1)
	{
		cout << "_________________" << endl;
		cout << " |        |      " << endl;
		cout << " |        |      " << endl;
		cout << " |        0      " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;

	}

	if (guessLimit == 2)
	{
		cout << "_________________" << endl;
		cout << " |        |      " << endl;
		cout << " |        |      " << endl;
		cout << " |        0      " << endl;
		cout << " |        |      " << endl;
		cout << " |        |      " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;

	}

	if (guessLimit == 3)
	{
		cout << "_________________" << endl;
		cout << " |        |      " << endl;
		cout << " |        |      " << endl;
		cout << " |        0      " << endl;
		cout << " |       /|      " << endl;
		cout << " |        |      " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;

	}

	if (guessLimit == 4)
	{
		cout << "_________________" << endl;
		cout << " |        |      " << endl;
		cout << " |        |      " << endl;
		cout << " |        0      " << endl;
		cout << " |       /|\\    " << endl;
		cout << " |        |      " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;

	}

	if (guessLimit == 5)
	{
		cout << "_________________" << endl;
		cout << " |        |      " << endl;
		cout << " |        |      " << endl;
		cout << " |        0      " << endl;
		cout << " |       /|\\    " << endl;
		cout << " |        |      " << endl;
		cout << " |       /       " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;

	}

	if (guessLimit == 6)
	{
		cout << "_________________" << endl;
		cout << " |        |      " << endl;
		cout << " |        |      " << endl;
		cout << " |        0      " << endl;
		cout << " |       /|\\    " << endl;
		cout << " |        |      " << endl;
		cout << " |       / \\    " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;
		cout << " |               " << endl;

	}

}


//definition of showSolved
void showSolved(char word[], char guesses[])
{
	//collect the size of the guesses and the actual word

	int size_word = strlen(word);
	int size_guesses = strlen(guesses);
	

	//Store properly guesses letters in an array &
	//Shows the letters correctly guessed

	char correctGuess[] = "";
	
	for(int i = 0; i < size_word; i++)
		for (int j = 0; j < size_guesses; j++)
		{
			if (word[j] == guesses[i])
				cout << "You got a letter right";
		}

}
You must loop through the word letter by letter and see if any of the letters matches the player's guess. If so, then simply place this letter into an array (although I'd rather use a vector instead of a fixed size array).

So something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::string theWord = "Hello";
std::vector<char> correctGuess(theWord.size(), '-'); // Creates dynamic array of theWord.size() and initialize each element to - for now.
char guess = ' ';

std::cin >> guess; // Get player guess
		for (unsigned int i = 0; i < theWord.size(); ++i) // Loop through the letters
		{
			if (guess == theWord[i]) // Check to see if guess is contained in the word
			{
				correctGuess[i] = guess; // If so, store the player's guess into the array
			}
		}
		
                // Display guessed letters
		std::cout << "Letters guessed: \n";
		for (unsigned int i = 0; i < correctGuess.size(); ++i)
		{
			std::cout << correctGuess[i] << "\n";
		}


Obviously you'll have to chuck that in within a gameloop.
Last edited on
closed account (48T7M4Gy)
Maybe you can adapt this. I assume you are not allowed to use <strings>, but the idea is easily adapted if you can.

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
#include <iostream>

using namespace std;

int main()
{
    char* word = "rainbow";
    int word_length = strlen(word);
    
    char* success_so_far = new char[word_length];
    
    char ch;
    int no_successes = 0;
    
    while(cout << "Please enter a letter: " && cin >> ch )
    {
       if(strchr(word,ch))
       {
           cout << "Bingo!\n";
           success_so_far[no_successes] = ch;
           no_successes++;
       }
        else
            cout << "Letter not found!\n";
        
        cout << "Found so far: " << success_so_far << '\n';
    }
    
    delete [] success_so_far; 
// the most important Uk marine amd't (hence value of smart pointers)
    
    return 0;
}
Please enter a letter: y
Letter not found!
Found so far: 
Please enter a letter: r
Bingo!
Found so far: r
Please enter a letter: t
Letter not found!
Found so far: r
Please enter a letter: o
Bingo!
Found so far: ro
Please enter a letter: w
Bingo!
Found so far: row
Please enter a letter:
Last edited on
If this is some sort of an assignment you were given and you are being restricted to using just fixed size arrays, then you can go with Kemort's example but remember to deallocate the memory that's on the heap (or freestore).
Topic archived. No new replies allowed.