Hangman Program loop

the first part of the while loop is working it will display the " is in the word to guess." However when I enter an incorrect letter it does not display the " is not in the word to guess" instead it displays "You guessed the letter." What is wrong with my logic?

[#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

const string boardOne = " ------|\n | |\n |\n |\n |\n -------\n\n";
const string boardTwo = " ------|\n | |\n 0 |\n |\n |\n -------\n\n";
const string boardThree = " ------|\n | |\n 0 |\n | |\n |\n -------\n\n";
const string boardFour = " ------|\n | |\n 0 |\n-| |\n |\n -------\n\n";
const string boardFive = " ------|\n | |\n 0 |\n-|- |\n |\n -------\n\n";
const string boardSix = " ------|\n | |\n 0 |\n-|- |\n \\ |\n -------\n\n";
const string boardSeven = " ------|\n | |\n 0 |\n-|- |\n/ \\ |\n -------\n\n";

int main()
{
// Declarations
string fileWord;
ifstream inFile;
int wrongGuess = 0;
char letterGuess = 0;
bool found = true;


// while loop to get the last word from file
inFile.open("hangman.dat");
while (!inFile.eof())
{
cout << fileWord << endl;
inFile >> fileWord;
}

inFile.close();

//capitalize the word to use in the game of hangman
std::transform(fileWord.begin(), fileWord.end(), fileWord.begin(), toupper);
cout << "\nWord to Guess: " << fileWord << endl;

cout << "\n" << boardOne << endl;

while (wrongGuess != 6)
{
cout << "\nEnter a letter to guess: ";
cin >> letterGuess;
letterGuess = toupper(letterGuess);
cout << "You guessed the letter: " << letterGuess << endl;

for (int i = 0; i < fileWord.length(); i++)
{
if (fileWord[i] == letterGuess)
{
cout << "\n" << letterGuess << " is in the letter to guess." << endl;
found = true;
}

}
// if not found - increment wrong guesses
if (!found)
{
wrongGuess++;
cout << letterGuess << " is not in the word to guess." << endl;
}

//print the board that corresponds to the wrongGuess

}


cout << "\n\n";
system("pause");
return 0;
}
]
Last edited on
@rfallon

Not sure if this is your only problem, but you never set found to false. You could add that right after the letterGuess input. You start with found being true, so even if the letter is NOT in the word, nothing is told.
@rfallon

You're welcome. I'm glad it helped.
Topic archived. No new replies allowed.