reading from file not working-hangman game

Hello,

For some reason, I can't seem to find out why the file won't open so I can read from it. I want to read from it, and put each line in a string for hangman game. This is so people can add there own words through a txt document.

Here is the ifstream segment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pressEnter("continue");
    vector <string> wordBank;
    ifstream fin;
    fin.open("wordbank.txt");
    string tempWord;
    if (fin.is_open())
    {
        cout << "Successfully opened Filed!" << endl;
        pressEnter("continue");
        while(fin >> tempWord)
        {
            wordBank.push_back(tempWord);
        }
        fin.close();
        wordSelect(wordBank);
    }
    else
    {
        cout << "No wordBank file found.  Using default words!" << endl;
        pressEnter("continue");
    }



-and the whole code is below. Any feedback for the code is appreciated. Thanks in advance
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>

using namespace std;

vector<char> board;
vector<string> body (6);
vector<char> wrongChar (6);
int correct=-1;
void menu();

string wordSelect(vector<string>& wordBanktxt)
{
    string word;
    if (wordBanktxt.size()>0)
    {
        srand(time(0));
        int random=rand()%(wordBanktxt.size()+1)+0;
        word=wordBanktxt[random];
    }
    else
    {
        string wordBank[10]={"campaign","idiom","converse","ignorance","manufacture","document","individual","literate","assistance","execution"};
        srand(time(0));
        int rD=rand()%(10+1)+0;
        word=wordBank[rD];
    }
    return word;
}

void pressEnter(string conWord)
{
    cout << string(3,'\n'); // MAKES SCREEN CLEANER AND MORE READABLE
    cout << "Press 'Enter' to " << conWord << "." << endl;  //conWord is the action
    cin.get(); //TAKES USER'S INPUT
    system("clear"); //CLEARS SCREEN
}

void help()
{
    system("clear");
    cout << "**********" << endl;
    cout << "***Help***" << endl;
    cout << "**********" << endl;
    string(3,'\n');

    cout << "\t WHERE U PUT THE FUCKING RULES" << endl;
    cin.ignore();
    pressEnter("go back to menu");
}
void promptLose(string word)
{
    system("clear");
    cout << "##  ##  ## ##" << endl;
    cout << "##  ##  ## ##" << endl;
    cout << "##  ##  ## ##" << endl;
    cout << "#############" << endl;
    cout << "|###########|" << endl;
    cout << "| ######### |" << endl;
    cout << "|           |" << endl;
    cout << "| (X)   (X) |" << endl;
    cout << "|     >     |" << endl;
    cout << "|   _____   |" << endl;
    cout << "|___________|" << endl;

    cout << "Sorry... You died because of your stupidity!  The word was '" << word << "'!" << endl;
    pressEnter("go back to menu");
}

void promptWin()
{
    cout << string(3, '\n');
    cout << "Congrats!  You got the word so you didn't die!" << endl;
    pressEnter("go back to menu");
}

char setBoard(int length)
{
    for(int i=0; i<6; i++)
    {
        wrongChar[i]=' ';
    }

    board.resize(length);
    for(int i=0; i<board.size(); i++)
    {
        board[i]='_';
    }
    for(int i=0; i<body.size(); i++)
    {
        body[i]=' ';
    }
}

void printBoard(int incorrect)
{
    if (incorrect>0)
    {
        if (incorrect==1)
        {
            body[0]="0";
        }
        if (incorrect==2)
        {
            body[1]="|";
        }
        if (incorrect==3)
        {
            body[2]="/";
        }
        if (incorrect==4)
        {
            body[3]="\b\\ ";
        }
        if (incorrect==5)
        {
            body[4]="/";
        }
        if (incorrect==6)
        {
            body[5]="\\";
        }
    }
    system("clear");
    cout << "\t " << "|========||" << endl;
    cout << "\t " << "|        ||" << endl;
    cout << "\t " << body[0] << "        ||" << endl;
    cout << "\t" << body[2] << "";
    cout << body[1] << " ";
    cout << body[3] << "      ||" << endl;
    cout << "\t" << body[4] << " ";
    cout << body[5] << "       ||" << endl;
    cout << "\t" <<"          ||" << endl;
    cout << "\t" <<"     =========" << endl;

    cout << endl;

    for(int i=0; i<board.size(); i++)
    {
        cout << board[i];
        cout << " ";
    }
    cout << endl;
    cout << endl;
    cout << "Incorrect Letters" << endl;
    cout << "___________________" << endl;
    cout << "| "<< wrongChar[0] <<"| "<< wrongChar[1] <<"| "<< wrongChar[2] <<"| "<< wrongChar[3] <<"| "<< wrongChar[4] <<"| "<<wrongChar[5]<<"| "<< endl;
    cout << "-------------------" << endl;
}

int replaceChar(string word,char guess,int tempCorrect,int incorrect)
{
    int numLetter=0;
    int length=word.length();
    vector <char> guessRep (length);
    for(int i=0; i<length; i++)
    {
        if (word[i]==guess)
        {
            numLetter++;
            board[i]=guess;
        }
    }
    if (numLetter==1)
    {
        cout << "There is " << numLetter << " " << guess << "!" << endl;
        tempCorrect++;
        correct=0;
    }
    if (numLetter==0)
    {
        cout << "There are no " << guess << "'s!" << endl;
        correct=1;
        if (incorrect<=6)
        {
        wrongChar[incorrect]=guess;
        }
    }
    if (numLetter>=2)
    {
        cout << "There are " << numLetter << " " << guess << "'s!" << endl;
        tempCorrect=tempCorrect+numLetter;
    }
    cin.ignore();
    pressEnter("continue");
    return tempCorrect;
}


int playGame()
{
    pressEnter("continue");
    vector <string> wordBank;
    ifstream fin;
    fin.open("wordbank.txt");
    string tempWord;
    if (fin.is_open())
    {
        cout << "Successfully opened Filed!" << endl;
        pressEnter("continue");
        while(fin >> tempWord)
        {
            wordBank.push_back(tempWord);
        }
        fin.close();
        wordSelect(wordBank);
    }
    else
    {
        cout << "No wordBank file found.  Using default words!" << endl;
        pressEnter("continue");
    }
    char guess;
    string word=wordSelect(wordBank);
    int length=word.length();
    int tempCorrect=0;
    int incorrect=0;
    int counter=-1;
    setBoard(length);

    while(true)
    {
        printBoard(incorrect);
        cout << "Guess: ";
        cin >> guess;
        tempCorrect=replaceChar(word,guess,tempCorrect,incorrect);
        if (correct==1)
        {
        incorrect++;
        }

        if(tempCorrect==length)
        {
            promptWin();
            break;
        }
        if(incorrect==6)
        {
            promptLose(word);
            break;
        }
    }

    if(tempCorrect>length || tempCorrect<0)
    {
        cout << "Uh Oh..." << endl;
        cout << "~~~~~~~~" << endl;
        cout << "The program came across a problem." << endl;
        cout <<"Exiting...";
        cout << "tempCorrect: " << tempCorrect << endl;
        cout << "---------------" << endl;
        cout << "incorrect: " << incorrect << endl;
        cout << "---------------" << endl;
        exit(0);
    }

}

void menu()
{
    int tempChoice;

    cout << "**************" << endl;
    cout << "*****MENU*****" << endl;
    cout << "**************" << endl;
    string(3,'\n');

    cout << "\t 1) Play" << endl;
    cout << "\t 2) Help" << endl;
    cout << "\t 3) Exit" << endl;
    cin >> tempChoice;

    if(tempChoice==1)
    {
        playGame();
    }

    if(tempChoice==2)
    {
        help();
    }

    if(tempChoice==3)
    {
        exit(0);
    }
    system("clear");
}

int main()
{
    cout << "#####################" << endl;
    cout << "#*******************#" << endl;
    cout << "#******Hangman******#" << endl;
    cout << "#*******************#" << endl;
    cout << "#####################" << endl;
    pressEnter("continue");

    while(true)
    {
        menu();
    }
    return 0;
}
Last edited on
First thing I'd check is if my file is in the working directory. I don't know what setup you have, but you might have to move the file into the /bin directory or something. Are you just getting "no wordbank file found" or whatever you have after else?

First thing I'd check is if my file is in the working directory. I don't know what setup you have, but you might have to move the file into the /bin directory or something. Are you just getting "no wordbank file found" or whatever you have after else?


Yea I double checked the directory and tried it in /bin as well to no prevail. It just prints "no wordbank file found".
Last edited on
Here is a minimal example that works for me. See if this works for you. You'll have to change the path to the absolute of your file. If you can get the absolute path to work, then you can start working on making your relative path work. You may just need a ./ in front of your file name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream fin;
    fin.open("C:/Mike/testfile.txt");  // change to the absolute path of your file
    if (fin.is_open())
    {
        std::string line;
        std::cout << "file opened" << std::endl;
        std::getline(fin, line);
        std::cout << line << std::endl;
        fin.close();
    }
    else
    {
        std::cout << "file not found" << std::endl;
    }
}


The only thing in my text file is

sample text


in a file called testfile.txt
Topic archived. No new replies allowed.