在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语

Console outputs a ton of gibberish after entering string with std::cin into while loop

本文关键字:控制台 输出 胡言乱语 字符串 循环 while 输入带 cin std      更新时间:2023-10-16

在进入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;
}