Hangman Can't Read next word from file

Hello All,

I could use some serious help. I feel that I'm missing only a minor thing, but it's causing my program to not work correctly. I am a first year comp. sci student, and this is the last project of the year...due tonight. So here's my problem, I can't read the next word from my text file storing the words. Every time you ask the user to play again, it only brings back the first word. I've tried a for loop at the beginning of the program, but then it just read through all my words, and displayed them all.

Can anyone please look at my code and tell me what I need to add in order to get the next word from my file should the person playing hangman wish to play again. For the record, the file only has 20 words of different lengths.

Thanks for your help in advance. Here's my code:





#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
void getword();
void usedLetters(int n);
using namespace std;

void getword()
{
char word[20];
char solution[20];
char guess;
char position;
int TotalIncorrect=0;
int done=0;
int flag=0;
int count=0;

ifstream infile;
infile.open("wordlist.txt");


cout << "Here's your word: \n";
cout << "\n";

infile >> word;


int size = strlen(word);

for (int i=0; i<size; i++)
{
solution[i]= '*'; //program now converts the word rabbit to ******
}
for (int i=size; i<20; i++)
{
solution[i]= ' '; //creates 20 spaces for word to fit in
}

for(int i=0; i<20; i++)
{
cout << solution[i]; //displays number of stars as per number of letters in word

}

cout << "\n";
cout << "\n";
cout << "It has " << size << " letters. \n";


//cout << word << endl; will print the word rabbit
//cout << "\n";
//cout << "\n";
while ((TotalIncorrect <= 6) && (done ==0))
{


cout << "\n";
cout << "\n";
cout << "Guess a letter. \n";
cout << "\n";
cin >> guess;
cout << "\n";


flag = 0;
for(int i=0; i<size; i++)
{

if(guess==word[i])
{
flag = 1;
solution[i]=guess;

}
}
if(flag == 0) cout<< "The guess is incorrect\n";
TotalIncorrect++;

for(int i=0; i<size; i++)
{
cout << solution[i];

}
flag =0;
for(int k=0; k<size; k++)
{
if(solution[k] != word[k])
flag=1;
}
if(flag==0)
done=1;

}

//cout << "\n"; maybe delete
//cout << "Guess another letter. \n";
//cout << "\n";
//cin >> guess;



//cout << "\n";
//cout << "\n";


}


/*void usedLetters(int n)
{
char usedLetter[10];
int i;
i=0;


n=usedLetter[i];

cout << "You have used the following letters so far: \n";
cout << usedLetter[i] << ", " << endl;
cout << "\n";
i++;

}*/

int main()
{
char flag = 'y';

while(flag=='y' || flag=='Y')
{

cout << "Welcome to Hangman!!! \n";
cout << "\n";
cout << "You will have six chances to correctly guess letters \n";
cout << "or solve the mystery word. If you can do it you win, if \n";
cout << "not, you lose. \n";
cout << "\n";
cout << "Let's play !!\n";
cout << "\n";
cout << "\n";

getword();

cout << "Do you want to play agian? \n";
cout << "\n";
cin >> flag;
}

return 0;
}

-Jeff
Hi,

The reason that you end up with the same word every time is that you your file reading code
is in getWord.

you should make a function something like

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
void	loadWords()
{

	word = new char*[20];

	for (int i=0;i<20;i++)
	{
		word[i] = new char[20];
	}

	ifstream infile;
	infile.open("wordlist.txt");
	
	int	wordCount = 0;

	while (infile.eof() == false)
	{
	
		infile >> word[wordCount];
		wordCount++;
		
	}

        infile.close();
}


I would also add the following variables with global scope {just above the first function}
1
2
3
4
5
6
7
char**	word;		
int		currentWord = 0;


//AND remove this from getWord
char         word[20];


And then you will need to access the characters using a line like

 
if(guess==word[currentWord][i])


And once finished with a word just increment 'currentWord' to get the next one.

Hope this was helpful
Shredded
Last edited on
Shredded,

You are the man(or woman)! I haven't tried it yet, but I will as soon as I get home. Thank you so much!! I really appreciate it. I'll let you know how it turns out. Again, thanks, this really helps me out a lot.

-Jeff
Topic archived. No new replies allowed.