提问者:小点点

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


在进入while循环以从玩家那里获得输入正确名称的确认之后,我得到了一个奇怪的输出到控制台的胡言乱语。

这个循环在难度等级确认检查时工作得很好,但是在游戏名称确认时却变得很疯狂。

您可以看到我在“setPlayerName”函数中进行的一些调试尝试,一切都很好。

编译很好没有错误,我很迷茫。 我已经尝试了。clear和。忽略,甚至在字符串输入之后,以及我能想到的所有其他东西,以找出问题所在。

#include <iostream>
#include <ctime>

void PrintGameIntro ()
{
    std::cout << "\nGame Intro Text\n";
} 

// 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;
}

共1个答案

匿名用户

结果=和==不是一回事。 感谢对@Stribor14的支持。

现在我知道编译器完全可以接受一个“坏的”(或者更确切地说,我认为是不完整的)while条件,它会导致

流浪汉流浪汉流浪汉流浪汉流浪汉流浪汉

一个堆栈溢出哈哈哈

删除了我放进去的调试步骤,它按预期工作

#include <iostream>
#include <ctime>

void PrintGameIntro ()
{
    std::cout << "\nGame Intro Text\n";
} 

// 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;
}