I need help for my hangman game

Can you help me?
I'm making a hangman game but it's not working as expected.
It's not filling the letters correctly.
Here is the code:

#include <iostream>
#include <windows.h>
#include <ctime>
#include <string>
using namespace std;
char letter;
int guesses = 0, matches, random, colour;
string name;
string word;
string words[] = {"apple", "tree", "boy", "smart", "keyboard", "computer", "english", "love", "peace", "school", "homework", "sniper"};
bool guessed = false;
void color();
int letterFill (char guess, string secretword, string &guessword);
void logo() {
cout << endl;
cout << " # # ### ## # #### ## ## ### ## # \n";
cout << " # # # # # # # # # # # # # # # # # \n";
cout << " ##### # # # # # # # # # # # # # # \n";
cout << " # # ##### # ## # ## # # ##### # ## \n";
cout << " # # # # # # #### # # # # # # \n";
cout << "\n by Aleksander Tsvetkov \n";
}
int main () {
SetConsoleTitle("Hangman");
srand(time(0));
random = rand() % 2;
colour = (rand() % 5) + 1;
color();
word = words[random];
string display(word.length(),'*');
while (guesses < 6 && guessed == false) {
logo();
cout << word;
cout << "\n\n\t\t\t\t Word: " << display;
cout << "\n\n\t\t\t Guess a letter: ";
cin >> letter;
if (letterFill(letter, word, display) == 0) {
guesses++;
switch (guesses) {
case 1 : {
cout << "*-------*\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
break;
}
case 2 : {
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
break;
}
case 3 : {
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| |\\ |\n";
cout << "| |\n";
cout << "| |\n";
break;
}
case 4 : {
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| /|\\ |\n";
cout << "| |\n";
cout << "| |\n";
break;
}
case 5 : {
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| /|\\ |\n";
cout << "| | |\n";
cout << "| / |\n";
break;
}
case 6 : {
cout << "You loose!\n";
cout << word;
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| /|\\ |\n";
cout << "| | |\n";
cout << "| / \\ |\n";
break;
guessed = true;
}
}
}
if (word == display) {
guessed = true;
system("cls");
cout << word << endl << endl << "Congatulations! You won the game!\n\n";
system("pause");
}
}
return 0;
}
void color() {
switch (colour) {
case 1 : system("color 1f");
break;
case 2 : system("color 3f");
break;
case 3 : system("color 6f");
break;
case 4 : system("color 2f");
break;
case 5 : system("color 4f");
break;
}
}
int letterFill (char guess, string secretword, string &guessword) {
for (int i = 0; i< secretword.length(); i++) {
if (guess == word[i])
return 0;
if (guess == word[i]) {
guessword[i] = guess;
matches++;
}
}
return matches;
}
Hi there!I think the problem here is these lines :
if (guess == word[i])
return 0;
It actually returns 0 whenever you have a match thus it never predicts the correct letter.Comment those out and it should work fine
another thing: did you realize that on line 33 t displays the word? I think you did this intentionally for testing but I'm not sure...
This code should work for 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
#include <iostream>
#include <windows.h>
#include <ctime>
#include <string>
using namespace std;
char letter;
int guesses = 0, matches, random, colour;
string name;
string word;
string words[] = {"apple", "tree", "boy", "smart", "keyboard", "computer", "english", "love", "peace", "school", "homework", "sniper"};
bool guessed = false;
void color();
int letterFill (char guess, string secretword, string &guessword);
void logo() {
cout << endl;
cout << " # # ### ## # #### ## ## ### ## # \n";
cout << " # # # # # # # # # # # # # # # # # \n";
cout << " ##### # # # # # # # # # # # # # # \n";
cout << " # # ##### # ## # ## # # ##### # ## \n";
cout << " # # # # # # #### # # # # # # \n";
cout << "\n by Aleksander Tsvetkov \n";
}
int main () {
SetConsoleTitle("Hangman");
srand(time(0));
random = rand() % 2;
colour = (rand() % 5) + 1;
color();
word = words[random];
string display(word.length(),'*');
while (guesses < 6 && guessed == false) {
logo();
cout << "\n\n\t\t\t\t Word: " << display;
cout << "\n\n\t\t\t Guess a letter: ";
cin >> letter;
if (letterFill(letter, word, display) == 0) {
guesses++;
switch (guesses) {
case 1 : {
cout << "*-------*\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
break;
}
case 2 : {
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
break;
}
case 3 : {
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| |\\ |\n";
cout << "| |\n";
cout << "| |\n";
break;
}
case 4 : {
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| /|\\ |\n";
cout << "| |\n";
cout << "| |\n";
break;
}
case 5 : {
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| /|\\ |\n";
cout << "| | |\n";
cout << "| / |\n";
break;
}
case 6 : {
cout << "You loose!\n";
cout << word;
cout << "*-------*\n";
cout << "| | |\n";
cout << "| @ |\n";
cout << "| /|\\ |\n";
cout << "| | |\n";
cout << "| / \\ |\n";
break;
guessed = true;
}
}
}
if (word == display) {
guessed = true;
system("cls");
cout << word << endl << endl << "Congatulations! You won the game!\n\n";
system("pause");
}
}
return 0;
}
void color() {
switch (colour) {
case 1 : system("color 1f");
break;
case 2 : system("color 3f");
break;
case 3 : system("color 6f");
break;
case 4 : system("color 2f");
break;
case 5 : system("color 4f");
break;
}
}
int letterFill (char guess, string secretword, string &guessword) {
for (int i = 0; i< secretword.length(); i++) {
if (guess == word[i]) {
guessword[i] = guess;
matches++;
}
}
return matches;
}
Topic archived. No new replies allowed.