Keeping track of used letters in hangman game

Hi am trying to make a hangman game in c++. Everything works so far but i can't seem to keep track of the used words in the game. As in,if a letter has already been entered before, the computer will have to display a message saying exactly that to the user,hence preventing them from using the same letter again.I have included the code below,please take a look at it and let me know how i can solve this issue.

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

using namespace std;

int main(){


  string words[] =
    {   "artistic",
        "recreational",
        "programmer",
        "indistinct",
        "sincere",
        "pulchritude",
        "legendary",
        "scientific",
        "oblivious",
        "tomorrow"
    };
   string word,unknownword,wordrevelation,playername,usedletters;
   int wordlength,numwrongofguesses,i,w,count;
   char letter,playagain;
   bool letterfound;

   cout << "Please enter your name: ";
   getline(cin,playername);
   cout << endl << endl;
   cout << "OKAY " << playername << " HERE WE GO!!!!!!!!!";
   cout << endl << endl;

    srand(time(0));
    w=rand()%10;
    word=words[w];

    wordrevelation=word;
    wordlength=word.size();

    for(i=0;i<wordlength;i++){
        unknownword[i]=word[i];
        word[i]='-';
    }
    cout << "OKAY HERES THE HIDDEN WORD "<< word << " AND IT IS " << wordlength << " LETTERS LONG" << endl << endl << endl;

    numwrongofguesses=8;
    count=0;
    bool Gameover=false;
    while(!Gameover){
        cout << "ENTER A LETTER " << playername << " : ";
        cin >> letter;
        
        

        letterfound=false;
        for(i=0;i<wordlength;i++){
            if(unknownword[i]==letter){
                letterfound=true;
                word[i]=letter;
                count++;
            }
        }
        cout << endl << endl;

        if(letterfound==true){
            word[i]=letter;
            cout << "THE LETTER " << letter << " EXISTS IN THE WORD" << endl << endl << endl;
            cout << "                              ";
            for(i=0;i<wordlength;i++){
                cout << word[i];
            }
            cout << endl << endl;
       }else{
             cout << "SORRY " << playername << ", " << letter << " DOES NOT EXIST IN THIS WORD" << endl << endl << endl;
             numwrongofguesses--;
            }

        if(numwrongofguesses==7){
            cout << "------------------" << endl;
            cout << "|                |" << endl;
            cout << endl << endl;
        }else if(numwrongofguesses==6){
            cout << "------------------" << endl;
            cout << "|                |" << endl;
            cout << "|                O" << endl;
            cout << endl << endl;
        }else if(numwrongofguesses==5){
            cout << "------------------" << endl;
            cout << "|                |" << endl;
            cout << "|                O" << endl;
            cout << "|                X" << endl;
            cout << endl << endl;
        }else if(numwrongofguesses==4){
            cout << "------------------" << endl;
            cout << "|                |" << endl;
            cout << "|                O" << endl;
            cout << "|               /X" << endl;
            cout << "|              /" << endl;
            cout << endl << endl;
        }else if(numwrongofguesses==3){
            cout << "------------------" << endl;
            cout << "|                |" << endl;
            cout << "|                O" << endl;
            cout << "|               /X\\" << endl;
            cout << "|              /   \\" << endl;
            cout << endl << endl;
        }else if(numwrongofguesses==2){
            cout << "------------------" << endl;
            cout << "|                |" << endl;
            cout << "|                O" << endl;
            cout << "|               /X\\" << endl;
            cout << "|              / | \\" << endl;
            cout << endl << endl;
        }else if(numwrongofguesses==1){
            cout << "------------------" << endl;
            cout << "|                |" << endl;
            cout << "|                O" << endl;
            cout << "|               /X\\" << endl;
            cout << "|              / | \\" << endl;
            cout << "|               /" << endl;
            cout << "|              /" << endl;
            cout << endl << endl;
        }else if(numwrongofguesses==0){
            cout << "------------------" << endl;
            cout << "|                |" << endl;
            cout << "|                O" << endl;
            cout << "|               /X\\" << endl;
            cout << "|              / | \\" << endl;
            cout << "|               / \\" << endl;
            cout << "|              /   \\" << endl;
            cout << "|----------|" << endl;
            cout << "|----------|" << endl;
            cout << endl << endl;
            cout << "                " << playername << " YOU HAVE BEEN HANGED!!!!" << endl << endl << endl;
            cout << "               THE WORD WAS " << wordrevelation << endl << endl << endl;
            Gameover=true;
            break;
        }

        if(numwrongofguesses==7 || numwrongofguesses==6 || numwrongofguesses==5 ||
           numwrongofguesses==4 || numwrongofguesses==3 || numwrongofguesses==2 ||
           numwrongofguesses==1 || numwrongofguesses==0){
               cout << "                                ";
               for(i=0;i<wordlength;i++){
                cout << word[i];
            }
        }
        cout << endl << endl;

        if(count==wordlength){
            cout << "CONGRATULATIONS " << playername << "! YOU GUESSED THE WORD!!! IT'S " << wordrevelation << endl << endl << endl;
            Gameover=true;
            break;
        }

        if(Gameover){
            cout << playername << " THANKS FOR PLAYING.HOPE TO SEE YOU AGAIN.";
            break;
        }
    }



}
closed account (48T7M4Gy)
One way is to replace the used letter by a zero or some other digit in the word. Or delete the letter from the word.
Maintain a set of available letters.

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
#include <iostream>
#include <string>
#include <set>
#include <cctype>

int main() {

    // ...

    static const std::string alphabet = "abcdefghijklmnopqrstuvwxyz" ; // #include <string>

    // #include <set> http://en.cppreference.com/w/cpp/container/set
    std::set<char> unused( alphabet.begin(), alphabet.end() ) ; // all letters are unused at the start

    bool Gameover=false;
    while(!Gameover){

        std::cout << "enter one of these letters [" ;
        for( char c : unused ) std::cout << c ;
        std::cout << "]: " ;

        char letter ;
        std::cin >> letter;
        letter = std::tolower(letter) ; // if uppercase, convert to lower case // #include <cctype>

        if( !std::isalpha(letter) ) { // if it is not a letter
            std::cout << "that is not a letter.\n" ;
        }


        else if( unused.find(letter) == unused.end() ) { // if this letter had been used earlier
            std::cout << "the letter " << letter << " was used earlier\n" ;
        }

        else {
            unused.erase(letter) ; // remove this letter from the set of unused letters

            bool letterfound=false;

            // rest of the code

        }
    }
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <stdio.h>

int main ()
{
    char ch;

    char letters[26];                    /// simple array to keep track
    for (int i =0; i < 26; i++ )
        letters[i] = '0';

    while(true)
    {
        std::cin >> ch;                 /// read in a letter
        letters[toupper(ch) - 65] = '1';/// sets letter as used

        for (int i = 0; i <26; i++)     /// just for show
            std::cout << letters[i];
        std::cout << std::endl;
    }
    return 0;
}
Topic archived. No new replies allowed.