Help with Hangman Project

I've been working on a hangman project. I have the whole thing done, but there is one part that isn't working as expected.

This part is to bring a word from a file into a character array so that the length of the word is determined and then each letter can be referenced against the users guess.

I used an end of file while loop to bring in the word from the file. For some reason it doesn't recognize the end of file but takes the last letter twice before it finally ends. It doesn't matter how long the word is or what the word is in the file. It always takes the last letter twice.

Here is the code I'm using. An ideas on why its not recognizing the end of file?

I tried bringing it in as a string, but then couldn't get it to manipulate each letter. strcmp and find didn't really work as intended either. I got syntax errors for conversion.

// put word into array for manipulation
// and Finding Word Length

inWord.open("../listout.txt");
wrdlength = 0;

cout << wrdlength << " ";
inWord.get(ch1);
word[wrdlength] = ch1;
cout << "This is ch1 for word array: " << ch1 << endl;
wrdlength++;

while (!inWord.eof())
{
cout << wrdlength << " ";
inWord.get(ch1);
word[wrdlength] = ch1;
wrdlength++;
cout << "This is ch1 for word array: " << ch1 << endl;
}// end while
closed account (L1T0RXSz)
Hi,
im also a beginner but i think you forgot the do statement before the while. Do u no how to do that? If u dont look it up. Im not sure but I'm pretty sure that's what it is.
Akash
a do while statement is a different kind of statement. This is a pretest statement while a do while is a post test statement.

Pretest means it tests the variable before it goes through the loop. A Posttest on the other hand tests after the loop is done.
Topic archived. No new replies allowed.