在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
Console outputs a ton of gibberish after entering string with std::cin into while loop
在进入while循环以从玩家那里确认输入了正确的名称后,我收到了一个奇怪的胡言乱语输出到控制台。
对于难度级别的确认检查,循环工作得很好,但对于游戏名称的确认,循环会变得疯狂。
您可以在";SetPlayerName";功能正常,一切正常。
编译很好,没有错误,我很失落。我已经尝试过.clear和.ignore,即使是在字符串输入之后,以及我能想到的其他一切,以确定问题所在。
#include <iostream>
#include <ctime>
void PrintGameIntro ()
{
std::cout << "nGame Intro Textn";
}
// Sets the level constraints for the game as well as getting the desired level and makes sure it's within the constraints so it can be confirmed and SET in SetGameDifficulty
int SelectGameDifficulty ()
{
int GameDifficulty = 0;
// Magic numbers for level constraints
const int MaxLevel = 10;
const int MinLevel = 1;
// Get desired level and check that it is within the level constraints
std::cout << "nSelect game difficulty 1-10:n";
std::cin >> GameDifficulty;
while (GameDifficulty > MaxLevel || GameDifficulty < MinLevel)
{
std::cout << "Please select a level from 1 to 10 or press CTRL-C to exit.n";
std::cin >> GameDifficulty;
}
return GameDifficulty;
}
// SETS the level difficulty after being SELECTED in SelectLevelDifficulty
int SetGameDifficulty ()
{
bool bCorrectlevel = false;
std::string CorrectLevelYesNo = "N";
while (bCorrectlevel != true)
{
const int GameDifficulty = SelectGameDifficulty();
std::cout << "nYou selected a game difficulty level of " << GameDifficulty << " is this correct? n Enter '(Y)es' or '(N)o'";
std::cin >> CorrectLevelYesNo;
if (CorrectLevelYesNo == "Yes" || CorrectLevelYesNo == "Y" || CorrectLevelYesNo == "yes" || CorrectLevelYesNo == "y")
{
bCorrectlevel = true;
return GameDifficulty;
}
else
{
std::cout << "Please select a difficulty and confirm it.";
}
}
}
std::string SetPlayerName()
{
std::cout << "nWhat is your name, agent?n";
std::string PlayerName;
std::cin >> PlayerName;
// BEGIN DEBUG STUFF
std::cin.clear();
std::cin.ignore();
std::cout << "Not gibberish?";
// END DEBUG STUFF
bool bCorrectName = false;
std::string CorrectNameYesNo = "N";
int NameLoopCount = 1;
// BEGIN DEBUG STUFF
int TestInt = 15;
std::cin >> TestInt;
std::cout << TestInt << PlayerName;
std::cout << "nOk, " << PlayerName << ", then. Did I get that right?n Enter '(Y)es' or '(N)o'n";
std::cin >> TestInt;
// END DEBUG STUFF
while (bCorrectName = false)
{
if(NameLoopCount > 1)
{
std::cout << "nOhh, my mistake. I must be getting deaf in my old age. What was it then?n";
std::cin >> PlayerName;
}
std::cout << "nOk, " << PlayerName << ", then. Did I get that right?n Enter '(Y)es' or '(N)o'n";
std::cin >> CorrectNameYesNo;
if (CorrectNameYesNo == "Yes" || CorrectNameYesNo == "Y" || CorrectNameYesNo == "yes" || CorrectNameYesNo == "y")
{
std::cout << "Alright then, " << PlayerName << ". Let's get started.";
return PlayerName;
}
NameLoopCount ++;
}
}
int main ()
{
PrintGameIntro();
const int GameDifficulty = SetGameDifficulty();
std::cin.clear();
std::cin.ignore();
const std::string PlayerName = SetPlayerName();
std::cout << "game set to level " << GameDifficulty << " and player name is " << PlayerName <<".";
return 0;
}
原来=和==不是一回事。感谢@stribor14的支持。
现在我知道编译器完全可以使用";坏的";(或者更确切地说,我认为是不完整的(而条件和它将导致
bummmmmmm-
堆栈溢出哈哈哈
删除了我放入的调试步骤,它按预期工作
#include <iostream>
#include <ctime>
void PrintGameIntro ()
{
std::cout << "nGame Intro Textn";
}
// Sets the level constraints for the game as well as getting the desired level and makes sure it's within the constraints so it can be confirmed and SET in SetGameDifficulty
int SelectGameDifficulty ()
{
int GameDifficulty = 0;
// Magic numbers for level constraints
const int MaxLevel = 10;
const int MinLevel = 1;
// Get desired level and check that it is within the level constraints
std::cout << "nSelect game difficulty 1-10:n";
std::cin >> GameDifficulty;
while (GameDifficulty > MaxLevel || GameDifficulty < MinLevel)
{
std::cout << "Please select a level from 1 to 10 or press CTRL-C to exit.n";
std::cin >> GameDifficulty;
}
return GameDifficulty;
}
// SETS the level difficulty after being SELECTED in SelectLevelDifficulty
int SetGameDifficulty ()
{
bool bCorrectlevel = false;
std::string CorrectLevelYesNo = "N";
while (bCorrectlevel != true)
{
const int GameDifficulty = SelectGameDifficulty();
std::cout << "nYou selected a game difficulty level of " << GameDifficulty << " is this correct? n Enter '(Y)es' or '(N)o'";
std::cin >> CorrectLevelYesNo;
if (CorrectLevelYesNo == "Yes" || CorrectLevelYesNo == "Y" || CorrectLevelYesNo == "yes" || CorrectLevelYesNo == "y")
{
bCorrectlevel = true;
return GameDifficulty;
}
else
{
std::cout << "Please select a difficulty and confirm it.";
}
}
}
std::string SetPlayerName()
{
std::cout << "nWhat is your name, agent?n";
std::string PlayerName;
std::cin >> PlayerName;
bool bCorrectName = false;
std::string CorrectNameYesNo = "N";
int NameLoopCount = 1;
while (bCorrectName == false)
{
if(NameLoopCount > 1)
{
std::cout << "nOhh, my mistake. I must be getting deaf in my old age. What was it then?n";
std::cin >> PlayerName;
}
std::cout << "nOk, " << PlayerName << ", then. Did I get that right?n Enter '(Y)es' or '(N)o'n";
std::cin >> CorrectNameYesNo;
if (CorrectNameYesNo == "Yes" || CorrectNameYesNo == "Y" || CorrectNameYesNo == "yes" || CorrectNameYesNo == "y")
{
std::cout << "Alright then, " << PlayerName << ". Let's get started.";
return PlayerName;
}
NameLoopCount ++;
}
}
int main ()
{
PrintGameIntro();
const int GameDifficulty = SetGameDifficulty();
const std::string PlayerName = SetPlayerName();
std::cout << "game set to level " << GameDifficulty << " and player name is " << PlayerName <<".";
return 0;
}
相关文章:
- C++:将控制台输出存储在宏中更好吗
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 控制台输出在 Qt5 中未正确显示
- 有人可以向我解释为什么控制台输出 0 吗?
- 使用 std::string_view 的子字符串控制台输出
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- 我如何阅读简单的控制台输出
- 是否可以在 RTL(从右到左)中制作控制台输出?
- 如何让控制台输出覆盖控制台而不是纯文本输出?
- 乱码控制台输出管道 stderr 到更多
- 将控制台输出写入.txt并在控制台上显示
- 当被一个输入分开时,有两个控制台输出同时显示
- VSCode C++调试捕获控制台输出
- 如何将控制台输出中的所有内容复制到文本文件?
- C++ 使用简单的代码同时写入文件和控制台输出
- 使控制台输出是否"verbose"的最快方法
- 将 ffmpeg 控制台输出重定向到C++中的字符串或文件
- C++ 控制台输出到 cmd 在重复十次后出错
- 从控制台输出读取 C++
- 如何将控制台输出的文本颜色更改为红色,但如果从Powershell或CMD运行,背景将保持相同的颜色