首页 > 解决方案 > 使用 std::cin 将字符串输入 while 循环后,控制台输出大量乱码

问题描述

在进入 while 循环以从玩家那里确认输入了正确的名称后,我在控制台收到了奇怪的乱码输出。

该循环在难度级别确认检查中工作得很好,但在播放名称确认时变得疯狂。

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

编译很好,没有错误,我很迷茫。我什至在字符串输入之后尝试了 .clear 和 .ignore 以及我能想到的所有其他内容来确定问题所在。

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

标签: c++while-loopconsolestd

解决方案


原来 = 和 == 不是一回事。感谢@stribor14 的支持。

现在我知道编译器在条件“坏”(或者更确切地说,我认为不完整)的情况下完全没问题,它会导致

呸呸呸呸呸

堆栈溢出哈哈哈

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

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

推荐阅读