Reputation: 1860
I am currently working on a program (hangman) and am having problems comparing the user input (char) to the word to be guessed (string) to determine if the guessed letter is in the word or not.
#include <iostream>
#include <fstream> // ifstream and ofstream
#include <iomanip> // input/output manipulation
#include <cctype> // toupper() function
#include <cstring>
#include <string> // strings
using namespace std;
#include "MyFuncts.h" // programmer defined includes
//ASCII ART FROM: http://ascii.co.uk/art/hangman by Manus O'Donnell
int main()
{
ifstream inFile; // used to read from the file
string stage0;
string stage1;
string stage2;
string stage3;
string stage4;
string stage5;
string stage6;
string word; // convert this to array down the road
char guess;
int numWrong = 0;
bool found = true;
//STAGE0
stage0 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||\n| |/\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n";
//STAGE1
stage1 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n";
//STAGE2
stage2 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /Y . . Y\\\n| | | |\n| | | . |\n| | | |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n";
//STAGE3
stage3 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /Y . . Y\\\n| | // | |\n| | // | . |\n| | ') | |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n";
//STAGE4
stage4 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /Y . . Y\\\n| | // | | \\\\\n| | // | . | \\\\\n| | ') | | (`\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n";
//STAGE5
stage5 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /Y . . Y\\\n| | // | | \\\\\n| | // | . | \\\\\n| | ') | | (`\n| | ||'\n| | ||\n| | ||\n| | ||\n| | / |\n\"\"\"\"\"\"\"\"\"\"|_`-' |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n";
//STAGE6 - GAME OVER
stage6 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /Y . . Y\\\n| | // | | \\\\\n| | // | . | \\\\\n| | ') | | (`\n| | ||'||\n| | || ||\n| | || ||\n| | || ||\n| | / | | \\\n\"\"\"\"\"\"\"\"\"\"|_`-' `-' |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n";
inFile.open("hangman.dat");
if (!inFile)
{
cout << "Error opening file for reading\n";
system("pause");
return 1;
}// end if
inFile >> word; // convert these into an array as well
while (!inFile.fail()) // .fail is better than .eof as it catches more issues
{
cout << "Word: " << word << endl;;
inFile >> word;
}
inFile.close();
word = caseChanger(word, true);
//INPUT
cout << "Word to Guess: " << word << endl << endl;
//PROCESS & OUTPUT
while (numWrong <= 6)
{
if (numWrong == 0)
cout << stage0 << endl;
else if (numWrong == 1)
cout << stage1 << endl;
else if (numWrong == 2)
cout << stage2 << endl;
else if (numWrong == 3)
cout << stage3 << endl;
else if (numWrong == 4)
cout << stage4 << endl;
else if (numWrong == 5)
cout << stage5 << endl;
else
cout << stage6 << endl;
cout << "Please enter a letter to guess: ";
cin >> guess;
guess = toupper(guess);
cout << "You entered: " << guess << endl;
for(int i = 0; i < word.length(); i++)
{
if(word[i] == guess)
found = true;
else
found = false;
}
if (found)
{
cout << guess << " is in the word to guess." << endl;
}
else
{
cout << guess << " is NOT in the word to guess." << endl;
numWrong = numWrong++;
}
}
cout << "\n\n";
system("pause");
return 0;
}
For some reason, when I enter a letter that is part of the word, this still says it is not, and increments the number of incorrect guesses (numWrong).
Could use afresh set of eyes, as I have no clue why this is not currently working.
Thanks!
Upvotes: 0
Views: 521
Reputation: 1796
Just make found = false
once you done with your operation when it is true.
if (found)
{
cout << guess << " is in the word to guess." << endl;
found = false; // Get it reset for the next loop.
}
Upvotes: 1
Reputation: 4006
The problem is in your loop to determine if the letter is correct. If it finds the correct letter, the loop doesn't end, and continues going through all the letters, which changes it back to false.
found = false; //Move this here to set it as false default before the loop starts
for(int i = 0; i < word.length(); i++)
{
if(word[i] == guess)
{
found = true;
break; //Add this here to exit the loop if letter is found
}
}
Upvotes: 2