[SOLVED] Various Problems with HANGMAN Program

SOLVED

Thanks!
Last edited on
What code do you have so far?
I didn't actually have a specific code. I tried all sorts of stuff from the internet, including some from source codes.

Here's my example using the site's one though. (Also, I need to know if an array is good to hold the words for processing. Will it be too big, since it's over 80,000 words?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
string word_array[89523];
string words;
ifstream myFile ("EnglishWords.txt");
if (myFile.is_open())
{
while (!myFile.eof())
{
getline (myFile, line);
word_array[89523] = { line };
}
myFile.close();
}
else
{
cout << "Unable to open or locate 'EnglishWords.txt'" << endl;
}
return(0);
}


This is only the code for trying to get the words into an array (or something to hold the words). But of course, it won't work.
Last edited on
Well, this is wrong for a start:

word_array[89523] = { line };

for two reasons:

1. The value 89523 is outside the bounds of the array - and would probably cause an access violation. The array bounds are
word_array[0] upto and including word_array[89522]

2. Evenif you had used a value within the bounds of the array, you are simply using the same array subscript over and over again.
80000 words at an average of 10 letters per word is 800 KiB, without counting structure overhead.

word_array[89523] = { line };
What?

A vector will be better suited for this task. You don't have to worry about how many line there actually are in the file.
Would my method of getting the lines work?
Yes, but the way you are using it you will want (need) to use a vector.
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    string line;
    vector<string>words;
    ifstream myFile ("EnglishWords.txt");
    if (myFile.is_open())
    {
        while (!myFile.eof())
        {
            getline (myFile, line);
            words.push_back(line);
        }
    }
    else
    {
        cout << "Could not locate or open 'EnglishWords.txt'" << endl;
    }
    cout << words[5] << endl;
}


I tried again using a vector (with a tutorial from cprogramming.com). This code is used to just try and get the words into a vector first, but of course, it doesn't work. I think I don't really understand how vectors work and might need some help.

Thanks!
80,000 words! Use a set (rather than a vector) for fast look ups; assuming that all you want to find out is if the word exists or not...

Anyway, add "return 0;" at the end and make sure you have EnglishWords.txt in the right directory and that looks like it'll work fine. Post your errors/results when you claim that it is not working so that we may be more helpful.

Also, add "myFile.close();" after the while loop.
Last edited on
SOLVED
Last edited on
SOLVED

My final production -

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
//Code source mostly based on HANGMAN by Sam Paterson.
//You may contact him at <peabodyenator@gmail.com>.
//Actual Hangman.cpp coded by Bluezor or Jackie.
//You may contact me at <***.******@gmail.com>.

/////////////////////////////////////////////////////////

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <ctime>
using namespace std;

vector <string> words;
vector <string> board;
vector <pair <pair <size_t, size_t>, char > > body;
set <char> used_letters;
string word;
string word_letters;
int randomNumber = rand() % 89523;
bool completed_loading = false;

inline void TOLOWER (string& st)
{
    for (size_t i = 0; i < st.size(); i++)
    {
        st[i] = tolower(st[i]);
    }
}

inline void check_cin()
{
    if (!cin)
    {
        if (cin.eof())
        exit(EXIT_SUCCESS);
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

void readFile()
{
    string line;
    ifstream myFile ("EnglishWords.txt");
    if (myFile.is_open())
    {
        while (!myFile.eof())
        {
            getline (myFile, line);
            words.push_back(line);
        }
        myFile.close();
        completed_loading = true;
    }
    else
    {
        cout << "Could not locate or open 'EnglishWords.txt'." << endl;
        cout << "Press ENTER to continue...";
        cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }
}

void fill_place_holders()
{
    for (size_t i = 0; i < word.size(); i++)
    word_letters.append("-");
}

void pickWord()
{
    word = words[randomNumber];
}

void print_board()
{
    vector <string>::iterator it;
    for (it = board.begin(); it != board.end(); it++)
    {
        cout << *it << endl;
    }
    cout << endl << endl << "Word: " << word_letters << endl;
    cout << "Used letters: ";
    set <char>::iterator set_it;
    for (set_it = used_letters.begin(); set_it != used_letters.end(); set_it++)
    {
        cout << *set_it << " " << endl;
    }
}

void create_board_and_body()
{
    board.push_back ("-----");
    board.push_back ("    |");
    board.push_back ("    |");
    board.push_back ("    |");
    board.push_back ("    |");
    board.push_back ("   /|\\");

    body.push_back(make_pair(make_pair(4,3), '\\'));
    body.push_back(make_pair(make_pair(4,1), '/'));
    body.push_back(make_pair(make_pair(3,2), '|'));
    body.push_back(make_pair(make_pair(2,3), '\\'));
    body.push_back(make_pair(make_pair(2,2), '|'));
    body.push_back(make_pair(make_pair(2,1), '/'));
    body.push_back(make_pair(make_pair(1,2), '0'));
}

void add_part()
{
    if (!body.size())
    {
        cout << "The man got hanged!" << endl;
        cout << "You thoroughly mourn to realise the word was " << word << endl;
        cout << "Press ENTER to continue...";
        cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
        exit(EXIT_SUCCESS);
    }
    pair <pair <size_t, size_t>, char > part = body.back();
    body.pop_back();
    board[part.first.first][part.first.second] = part.second;
}

void users_turn()
{
    char letter;
    cout << "Enter a letter to guess." << endl;
    check_cin();
    cin >> letter;
    letter = tolower(letter);

    while (used_letters.find(letter) != used_letters.end() || !isalpha(letter))
    {
        cout << "Letter taken already, or bad letter. \n\n" << endl;
        cout << "Enter a letter to guess." << endl;
        check_cin();
        cin >> letter;
        letter = tolower(letter);
    }

    used_letters.insert(letter);
    bool found = false;
    for (size_t i = 0; i < word.size(); i++)
    {
        if (word[i] == letter)
        {
            word_letters[i] = letter;
            found = true;
        }
    }

    if (word_letters.find("-") == string::npos)
    {
        cout << "You got it! The word is " << word << "!" << endl;
        cout << "Congratulations! You saved the man from being hanged!" << endl;
        exit(EXIT_SUCCESS);
    }

    if (!found)
    add_part();
}

int main()
{
    srand(time(0));

    cout << "Choosing a word from 'EnglishWords.txt', please wait." << endl;
    readFile();
    pickWord();
    if (completed_loading = true)
    {
        cout << "COMPLETE!" << endl;
    }
    fill_place_holders();
    create_board_and_body();
    print_board();

    while(true)
    {
        users_turn();
        print_board();
    }

    cout << "Press ENTER to continue...";
    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    return(0);
}


Wow! That is some SERIOUSLY complicated code for me. :@.

NOTE: Original code posted in http://peabody.weeman.org/hangman.cpp.
Last edited on
Topic archived. No new replies allowed.