我目前正在处理一个程序(hangman),并且在将用户输入(char)与要猜测的单词(字符串)进行比较时遇到问题,以确定猜测的字母是否在单词中。
#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;
}
出于某种原因,当我输入一个字母的一部分时,这仍然表示它不是,并增加错误猜测的数量(numWrong)。
可以重新使用一组眼睛,因为我不知道为什么它目前没有用。
谢谢!
答案 0 :(得分:2)
问题出在你的循环中,以确定字母是否正确。如果找到正确的字母,则循环不会结束,并继续遍历所有字母,这会将其更改为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
}
}
答案 1 :(得分:1)
在完成操作后,只需执行found = false
即可。
if (found)
{
cout << guess << " is in the word to guess." << endl;
found = false; // Get it reset for the next loop.
}