逻辑错误,需要帮助
Logic error, assistance required
>我在这个应用程序上遇到了一个逻辑错误。这是一个单词混乱的应用程序,显示一个混乱的单词,并询问玩家是否愿意在他们猜对后再次玩。
当我告诉应用程序我不想再玩时,它无论如何都会继续执行序列。我有一种感觉,它对我的嵌套很糟糕。
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"wall", "Are you banging your head against something?"},
{"jumble", "Its what this game is all about."},
{"glasses", "You might need these to read this text."},
{"labored", "Going slowly, is it?"},
{"persistent", "Keep at it."},
};
srand(static_cast<unsigned int>(time(0)));
cout << "ttWelcome to Word Jumble!nn";
cout << "Unscramble the the letters to make the word!n";
cout << "Enter 'hint' for a hintn";
cout << "Enter 'quit' to quit the gamenn";
const int MAX_LEVEL = NUM_WORDS - 1;
int totalScore = 0;
for (int level = 0; level <= MAX_LEVEL; ++level)
{
string theWord = WORDS[level][WORD]; // Word to guess
string theHint = WORDS[level][HINT]; // Word hint
char playAgain;
string jumble = theWord; //Jumbled version of the word
int length = jumble.size();
int score = jumble.size() * 10;
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}
cout << jumble << endl;
string guess;
cout << "nYour Guess: ";
cin >> guess;
while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
score = score / 2;
}
else
{
cout << "nnSorry thats not it.nn";
}
cout << "nnYour Guess: nn";
cin >> guess;
}
if (guess == theWord)
{
cout << "Thats it! You guessed it!tYou scored: " << score << "nn";
cout << "Would you like to play again? (y/n): ";
cin >> playAgain;
if (playAgain = 'y')
{
continue;
}
else if (playAgain = 'n')
{
cout << "Your total score is: " << totalScore << endl;
break;
}
}
else if (guess == "quit")
{
if (totalScore > 0)
{
cout << "Your total score is: " << totalScore << endl;
}
break;
}
}
cout << "nGoodbye.";
return 0;
}
当playAgain
与'y'
和'n'
进行比较时,你只有一个等号,导致第一个('y'
)总是执行而不是实际的选择,因为'y'
的值不是0。
要解决此问题,它们应该是:
if (playAgain == 'y') //note ==
{
continue;
}
else if (playAgain == 'n') //note ==
{
cout << "Your total score is: " << totalScore << endl;
break;
}
此外,如果您打开了警告,任何理智(更现代)的编译器都应该警告您。一定要打开它们并注意它们。
我认为您将需要==用于您的PlayAgain问题。我经常犯错误。
相关文章:
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- C++:需要帮助了解运算符重载错误
- C++ DLL 运行时错误"abc.dll is not a valid WIN32 application" 。请帮助解决这个问题
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 需要一些帮助来查找我的程序中的逻辑错误
- 我用C++做了一个计算器,但我对"type of calculator"的要求循环了两次。有人可以帮助指出我的错误吗?
- 霍纳斯定律需要代数帮助,显示错误的答案 c++
- 错误:E0018 需要支架 - 需要帮助
- 任何人都可以在带有JNI的Android Studio上帮助我解决此错误,是否缺少一些内容?
- 需要帮助修复分段错误(核心转储)
- 需要帮助识别简单多线程代码中的错误
- 函数和指针帮助:程序生成,但崩溃,没有任何特定错误
- 需要帮助以了解错误消息
- 功能错误中的表达语法帮助!C/C
- For-Loop帮助获得错误的输出
- 帮助错误 C2259:无法实例化抽象类
- 调试帮助:C++错误
- 链接器错误-请帮助:错误LNK2001:无法解决的外部符号
- OpenCV帮助-错误:没有匹配' operator[] '(操作数类型是' cv::Mat '和' int ').&l
- 投票程序CPP帮助(错误输出)