C++ C++;刽子手游戏,如何制作;对",;信笺
我试图做一个刽子手项目,但我的代码不起作用。每当我输入正确的字母时,代码就会告诉我它是错误的(即使它是对的)。不太清楚为什么-代码在某个时候起作用了,但我改变了一些东西,现在我不知道为什么它不起作用。因此,这可能是一个简单的修复,但我只是没有看到它 任何帮助都将不胜感激C++ C++;刽子手游戏,如何制作;对",;信笺,c++,C++,我试图做一个刽子手项目,但我的代码不起作用。每当我输入正确的字母时,代码就会告诉我它是错误的(即使它是对的)。不太清楚为什么-代码在某个时候起作用了,但我改变了一些东西,现在我不知道为什么它不起作用。因此,这可能是一个简单的修复,但我只是没有看到它 任何帮助都将不胜感激 #include <iostream> using namespace std; int letterFill (char, string, string&); int main() { st
#include <iostream>
using namespace std;
int letterFill (char, string, string&);
int main()
{
string name;
int maxAttempts = 5;
int wrongGuesses;
char letter;
srand(time(0));
const string wordList[15] = { "hanukkah", "sparklers", "mistletoe", "menorah", "presents", "reindeer",
"kwanzaa", "snowman", "eggnog", "celebration", "yuletide", "resolution", "nutcracker", "ornaments", "gingerbread" };
string correctWord = wordList[rand() % 15];
string unknown(correctWord.length(),'*');
cout << correctWord << endl;
cout << "Welcome to a fun game of winter holiday hangman! What is your name? " << endl;
cin >> name;
cout << name <<", there are some simple things you should know about this game before you start playing!" << endl;
cout << "You will be trying to guess a randomly selected word by typing in ONE letter at a time " << endl;
cout << "You will have " << maxAttempts << " tries before losing the game " << endl;
cout << "And remember, all of the words are winter holiday related. Good luck " << name <<"!" << endl;
cout << "*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*" <<endl;
while (wrongGuesses == 0)
{
cout << "Guess a letter" << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 1)
{
cout << "You have 4 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 2)
{
cout << "You have 3 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 3)
{
cout << "You have 2 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 4)
{
cout << "You have 1 guess left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 5)
{
cout << "Sorry " << name << " you have made 5 wrong guesses!" << endl;
cout << "Game over. Click any key to exit. Play again soon :) " << endl;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
system("pause");
return 0;
}
int letterFill (char guessLetter, string mysteryWord, string& guessWord)
{
int x;
int matches=0;
int lengthWord=mysteryWord.length();
for (x = 0; x< lengthWord; x++)
{
if (guessLetter == mysteryWord[x])
return 0;
if (guessLetter == mysteryWord[x])
{
guessWord[x] = guessLetter;
matches++;
}
}
return matches;
}
#包括
使用名称空间std;
int-letterFill(字符、字符串、字符串和);
int main()
{
字符串名;
int=5;
猜错;
字符字母;
srand(时间(0));
const string单词列表[15]={“光明节”、“火花”、“槲寄生”、“梅诺拉”、“礼物”、“驯鹿”,
“宽扎”、“雪人”、“蛋酒”、“庆典”、“圣诞节”、“决议”、“胡桃夹子”、“装饰品”、“姜饼”};
字符串correctWord=wordList[rand()%15];
字符串未知(correctWord.length(),'*');
cout您没有更新int letterFill()
函数中的字符串猜测词。只要看到匹配的字母,就返回,而不输入第二个if
语句
我假设您只想在完全更新猜测词
后返回,基于此,您要做的是迭代字符串,在找到匹配项时更新猜测词
,并在循环后进行检查
if(matches==0)返回0;
else返回匹配项;
您能解释更多内容吗(显示修复)关于你的陈述,请。看起来你的思路是对的。我正在研究,只是想把她最初的问题抛到一边。假设,因为她的名字是梅格?如果梅格是男性,那么我为这个假设道歉。你永远不会知道。我更喜欢使用性别不可知的称呼(πάντα)ῥεῖ 你知道;-)。这是一个小问题,别担心。如果(guessLetter==mysteryWord[x])
,请检查下面的行。测试将重复,并且第二个嵌套部分永远不会运行。