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?
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
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.