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.
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
/*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;
}
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.