为什么我的函数被读取,却变成了一个无限循环



你好,我遇到了一个问题,当我在用户输入"Y"开始游戏后调用我的函数时,cout被读取,但它变成了一个无限循环。当你输入"N"或一些不应该输入的东西时,它会很好地工作。我正在使用一个名为functions的头文件,如果与此有关的话,请将所有函数都放进去。我仍处于编程的早期学习阶段,遇到了很多减速带,只是不太确定该去哪里。感谢您的帮助。(附言:因为这个问题,我还没有开始使用gameStart((函数。这不是最终的结果。(

#ifndef FUNCTIONS_H;
#define FUNCTIONS_H
#include <iostream>
using namespace std;
void startScreen()
{
void gameStart();
char answer;
cout << "Welcome to __________nn";
cout << "This is my fisrt ever actual program I made out of my own free will lol.n";
cout << "It is a Text-Based Adventure game. In this game you will make a character,n";
cout << "and explore the land of Spelet, battling enemies, leveling up, getting loot,n";
cout << "and learning skills! You do not need to capitalize anything but your charactern";
cout << "name. If a question has (something like this), those are the choices for that n";
cout << "interaction! Thank you for trying out my terrible little game! :)n";
cout << "I really hope y'all enjoy it!nn";
cout << "Would you like to play?n";
cin >> answer;
do
{
if (answer == 'Y' || answer == 'y')
{
gameStart();
}
else if (answer == 'N' || answer == 'n')
{
cout << "Program will now close...n";
system("pause");
exit(0);
}
else
{
cout << "Enter a Y for yes or an N for no.n";
cout << "Would you like to play?n";
cin >> answer;
}
}
while (answer != 'N', 'n' || 'Y', 'y');
}
void gameStart()
{
cout << "n"BOOM-BOOM-BOOM..."nn" << endl;
}
#endif

也许您需要:

while (answer != 'N' && answer != 'n' && answer != 'Y' && answer != 'y')

逗号运算符不会执行您认为的操作。正如我的链接所说,它"丢弃了结果"。

您需要&&运算符(AND运算符(:

while (answer != 'N' && answer != 'n' && answer != 'Y' && answer != 'y');

相关内容

最新更新