Replacing String in Hangman Game

Hi, I'm making a hangman game.
All i need to do is after the user guesses the letter correctly it replaces the stars with that letter. I have been trying for the past 2 hours and cant get it.
So basically I have to do this. DisplayWord.replace(0, theWord(0)) but I don't know how to put it in.
Help would be greatly appreciated
This is my code:
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>


using namespace std;

int main() 
{
    
    const int MAX_WORD = 5; //max amount of words is 10
    const int MAX_GUESS = 12; //the biggest word has 12 letters therefore max amount of right guesses = 12
    srand(static_cast<unsigned int> (time(0))); //used to randomize
    int choice = (rand() % MAX_WORD); //the choice random
    string guess; //the guess the user makes
    int tries = 0; //amount of wrong tries he's taken
    int RightGuess = 0; //the amount of right guesses
    bool WordGuessed = false; //whether or not the word is guessed right (true or false)
    bool lost = false;
    const string WORD[MAX_WORD] = //array of possible words
    {
        {"world"},
        {"computer"},
        {"phone"},
        {"keyboard"},
        {"watch"},
    };

    string theWord = WORD[choice]; //theWord is a randomly chosen word from array of possible words

         string DisplayWord(theWord.length(),'*');

    const int ALPHABET = 26; //26 letters in the alphabet
    string alphabet[ALPHABET] //the alphabet (words user can choose from)
    {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

    //this is where the game starts, tells us how many letters are in the word
    cout << "Shayan's Hangman Game!" << endl;
    cout << "Up for a challenge? Guess the Word!"<< endl;
    
    if (theWord.length() == 5) {
        cout << "It is a 5 letter word" << endl;
        cout << DisplayWord << endl;
    }
    if (theWord.length() == 8) {
        cout << "It is a 8 letter word" << endl;
        cout << DisplayWord << endl;
    }
  
   
    do 
    {
        cout << "Guess one of the following: "; //the possible letters we can choose from
        for (int a = 0; a < 26; a++) 
        {
            cout << alphabet[a] << " ";
        }
        cin >> guess; //input the guess
        cout << endl << "you guessed: " << guess << endl; //the letter he guessed is displayed
            
                for (int w = 0; w < 26; w++) {
                if (alphabet[w] == guess) {
                    alphabet[w] = "";
                    if (theWord.find(guess) == string::npos) {
                        cout << "Wrong letter, guess again." << endl;
                        tries++;
                        
                    } else {
                        if (theWord[0] == guess[0]) {
                            cout << "you guessed the first letter" << endl;

                           DisplayWord.replace(0,0, theWord[0]);

                            RightGuess++;
                        }
                        if (theWord[1] == guess[0]) {
                            cout << "you guessed the second letter" << endl;
                            RightGuess++;
                        }
                        if (theWord[2] == guess[0]) {
                            cout << "you guessed the third letter" << endl;
                            RightGuess++;
                        }
                        if (theWord[3] == guess[0]) {
                            cout << "you guessed the fourth letter" << endl;
                            RightGuess++;
                        }
                        if (theWord[4] == guess[0]) {
                            cout << "you guessed the fifth letter" << endl;
                            RightGuess++;
                        }
                        if (theWord.length() >= 5 && theWord[5] == guess[0]) {
                            cout << "you guessed the sixth letter" << endl;
                            RightGuess++;
                        }
                        if (theWord.length() >= 6 && theWord[6] == guess[0]) {
                            cout << "you guessed the seventh letter" << endl;
                            RightGuess++;
                        }
                        if (theWord.length() >= 7 && theWord[7] == guess[0]) {
                            cout << "you guessed the eighth letter" << endl;
                            RightGuess++;
                        }
                        
                        }
                    }
                }
            
            
            cout << "guess again" << endl;
            if (alphabet[0] == "") {
                cout << "you already guessed a" << endl;
            } 
            if (alphabet[1] == "") {
                cout << "you already guessed b" << endl;
            } 
            if (alphabet[2] == "") {
                cout << "you already guessed c" << endl;
            } 
            if (alphabet[3] == "") {
                cout << "you already guessed d" << endl;
            } 
            if (alphabet[4] == "") {
                cout << "you already guessed e" << endl;
            } 
            if (alphabet[5] == "") {
                cout << "you already guessed f" << endl;
            } 
            if (alphabet[6] == "") {
                cout << "you already guessed g" << endl;
            } 
            if (alphabet[7] == "") {
                cout << "you already guessed h" << endl;
            } 
            if (alphabet[8] == "") {
                cout << "you already guessed i" << endl;
            } 
            if (alphabet[9] == "") {
                cout << "you already guessed j" << endl;
            } 
            if (alphabet[10] == "") {
                cout << "you already guessed k" << endl;
            } 
            if (alphabet[11] == "") {
                cout << "you already guessed l" << endl;
            } 
            if (alphabet[12] == "") {
                cout << "you already guessed m" << endl;
            } 
            if (alphabet[13] == "") {
                cout << "you already guessed n" << endl;
            } 
            if (alphabet[14] == "") {
                cout << "you already guessed o" << endl;
            } 
            if (alphabet[15] == "") {
                cout << "you already guessed p" << endl;
            } 
            if (alphabet[16] == "") {
                cout << "you already guessed q" << endl;
            } 
            if (alphabet[17] == "") {
                cout << "you already guessed r" << endl;
            } 
            if (alphabet[18] == "") {
                cout << "you already guessed s" << endl;
            } 
            if (alphabet[19] == "") {
                cout << "you already guessed t" << endl;
            } 
            if (alphabet[20] == "") {
                cout << "you already guessed u" << endl;
            } 
            if (alphabet[21] == "") {
                cout << "you already guessed v" << endl;
            } 
            if (alphabet[22] == "") {
                cout << "you already guessed w" << endl;
            } 
            if (alphabet[23] == "") {
                cout << "you already guessed x" << endl;
            } 
            if (alphabet[24] == "") {
                cout << "you already guessed y" << endl;
            } 
            if (alphabet[25] == "") {
                cout << "you already guessed z" << endl;
            }
        if (tries == 5){
             cout << "You just got Hanged. Better luck next time." << endl;
             lost = true;
             
           
            }
            
        if(theWord.length() == RightGuess){
            WordGuessed = true;
            cout << "Congratulations! You Won!" << endl;
             

        }
      
    } while (!WordGuessed && !lost);
   
    return 0;
    }
Last edited on
Please put your code in code tags to make the program much easier to read.
Last edited on
I changed the part that i need help with
Topic archived. No new replies allowed.