使用 getline() 将字符串输入程序会导致某种溢出到下一个输入

using getline() to input strings into the program is causing some kind of overflow into the next input

我的程序中有很多问题都与输入有关。 在程序中,我要求用户输入的第一件事是他们的名字,我用这个

cout << "Please tell me your name." << endl;
getline(cin, user_name);
cout << "Hello " << user_name << " and welcome to Fantasy Battle!" << endl;

其中 user_name 在别处声明为字符串变量。这部分似乎没有问题,因为以下消息正确显示在屏幕上

用户的下一个输入来自此代码

{
cout << "Hello, what would you like to do?" << endl;
cout << "1. Play" << endl << "2. Exit" << endl;
cout << "Please enter the number corresponding to your choice from the list 
above." << endl;
for(;;)
{
    if(cin >> menuChoice)
    {
        if(cin.get() == '.')
        {
            cin.clear();
            cin.ignore(10000, '\n');
        }
        if(menuChoice == 1 || menuChoice == 2)
            break;
        else
        {
            cout << "You did not enter a valid menu option. Please try 
            again." << endl;
            cin.clear();
            cin.ignore(100000, '\n');
        }
    }
    else
    {
        cout << "You did not enter a valid menu option. Please try again." 
        << endl;
        cin.clear();
        cin.ignore(100000, '\n');
    }
}
if(menuChoice == 2)
{
    return 2;
}
else
{
    //setup a fight code further down
}

第一个 运行 如果我输入 2 它将从 main 成功退出程序,或者如果输入 1 它将 运行 通过战斗功能。但是,如果我经历了 1 次战斗并返回要求我输入 1 或 2 进行播放或退出的程序,我可以无限次输入 2 并且它不会退出程序。我不确定是什么原因造成的。

for(;;)
    {
        game = menu();
        if(game == 2)
        {
            break;
        }
        else
        {
            fight();
        }
     }
     return 0;

我程序的 menu() 函数内的代码如下,其中包含我程序的其余输入。我正在使用 getline(cin, fighterName) 从用户那里获取一个字符串,用作他们想要创建的每个角色的名称 我遇到的问题是它开始只是在不询问的情况下将字符名称保存为空。

cout << "How many fighters should be on Team 1?" << endl;
//Input Validation
for(;;)
{
    if(cin.get() == '.')
    {
        cin.clear();
        cin.ignore(100000, '\n');
    }
    if(cin >> team1Size)
    {
        if(team1Size <= 0)
        {
            cout << "The team must be a size of at least 1 fighter. Please try again." << endl;
            cin.clear();
            cin.ignore(100000, '\n');
        }
        else
        {
            break;
        }
    }
    else
    {
        cout << "You did not enter a valid number. Please try again." << endl;
           cin.clear();
            cin.ignore(100000, '\n');
    }
}

cout << "How many fighters should be on Team 2?" << endl;
//Input Validation
for(;;)
{
    if(cin.get() == '.')
    {
        cin.clear();
        cin.ignore(100000, '\n');
    }
    if(cin >> team2Size)
    {
        if(team2Size <= 0)
        {
            cout << "The team must be a size of at least 1 fighter. Please try again." << endl;
            cin.clear();
            cin.ignore(100000, '\n');
        }
        else
        {
            break;
        }
    }
    else
    {
        cout << "You did not enter a valid number. Please try again." << endl;
           cin.clear();
            cin.ignore(100000, '\n');
    }
}

//Set up team 1 
cout << "Begin setup for team 1:" << endl << endl;
for(int i = 0; i < team1Size; i++)
{
    cout << "Which character type should fighter " << i+1 << " be?" << endl;
    cout << "1. Barbarian" << endl;
    cout << "2. BlueMen" << endl;
    cout << "3. Vampire" << endl;
    cout << "4. Medusa" << endl;    
    cout << "5. Harry Potter" << endl;
    cout << "Please enter the number corresponding to your choice from the list above." << endl;
    for(;;)
    {
        if(cin.get() == '.')
        {
            cin.clear();
            cin.ignore(100000, '\n');
        }
        if(cin >> fighterType)
        {
            if(fighterType < 1 || fighterType > 5)
            {
                cout << "You did not enter a valid choice. Please try again." << endl;
                cin.clear();
                cin.ignore(100000, '\n');
            }
            else
                break;
        }
        else
        {
            cout << "You did not enter a valid choice. Please try again." << endl;
            cin.clear();
            cin.ignore(100000, '\n');
        }
    }
    //Now that we have the desired type of the fighter we must add a fighter of the correct type to the linked list
    //representing team 1. We will do so by calling the add function of the linked list
    cout << "Please enter the name of this fighter." << endl;
    getline(cin, fighterName);
    if(fighterType == 1)
    {
        team1.addBack("Barbarian", fighterName);
    }
    else if(fighterType == 2)
    {
        team1.addBack("BlueMen", fighterName);
    }
    else if(fighterType == 3)
    {
        team1.addBack("Vampire", fighterName);
    }
    else if(fighterType == 4)
    {
        team1.addBack("Medusa", fighterName);
    }
    else
    {
        team1.addBack("HarryPotter", fighterName);
    }

}

cout << "Team 1 has been created!" << endl << endl;

//Set up team 2 
cout << "Begin setup for team 2:" << endl << endl;
for(int i = 0; i < team2Size; i++)
{
    cout << "Which character type should fighter " << i+1 << " be?" << endl;
    cout << "1. Barbarian" << endl;
    cout << "2. BlueMen" << endl;
    cout << "3. Vampire" << endl;
    cout << "4. Medusa" << endl;    
    cout << "5. Harry Potter" << endl;
    cout << "Please enter the number corresponding to your choice from the list above." << endl;
    for(;;)
    {
        if(cin.get() == '.')
        {
            cin.clear();
            cin.ignore(100000, '\n');
        }
        if(cin >> fighterType)
        {
            if(fighterType < 1 || fighterType > 5)
            {
                cout << "You did not enter a valid choice. Please try again." << endl;
                cin.clear();
                cin.ignore(100000, '\n');
            }
            else
                break;
        }
        else
        {
            cout << "You did not enter a valid choice. Please try again." << endl;
            cin.clear();
            cin.ignore(100000, '\n');
        }
    }
    //Now that we have the desired type of the fighter we must add a fighter of the correct type to the linked list
    //representing team 2. We will do so by calling the add function of the linked list
    cout << "Please enter the name of this fighter." << endl;
    getline(cin, fighterName);
    if(fighterType == 1)
    {
        team2.addBack("Barbarian", fighterName);
    }
    else if(fighterType == 2)
    {
        team2.addBack("BlueMen", fighterName);
    }
    else if(fighterType == 3)
    {
        team2.addBack("Vampire", fighterName);
    }
    else if(fighterType == 4)
    {
        team2.addBack("Medusa", fighterName);
    }
    else
    {
        team2.addBack("HarryPotter", fighterName);
    }
}


cout << "Team 2 has been created!" << endl << endl;

cout << "Let the fight begin!" << endl << endl;

return 0;

}

我的代码的最后一段输入如下,它只是要求用户输入一个字符 y 或 n,然后在输入 y 时执行一个函数。

cout << "Would you like to see the contents of the loserPile?" << endl;
    cout << "Please enter y for yes or n for no" << endl;
    for(;;)
    {
        if(cin >> displayLosers)
        {
            if(displayLosers != 'y' && displayLosers != 'n')
            {
                cout << "You did not enter either y or n. Please try again." << endl;
                cin.clear();
                cin.ignore(100000, '\n');
            }
            else
                break;
        }
        else
        {
            cout << "You did not enter either y or n. Please try again." << endl;
            cin.clear();
            cin.ignore(100000, '\n');
        }
    }
    if(displayLosers == 'y')
    {
        losers.displayPile();
    }

如果有人能指出我在获取用户输入时出错的地方,我将不胜感激,因为我运行正在尝试我所知道的事情。

您添加 if(cin.get() == '.')

会造成很多问题

>> 运算符会将输入字符串 "1." 转换为 1,如果您调用 ignore(...,'\n'). 和 [ 之前的任何其他字符=18=] 将被忽略。也没有必要测试 if(cin >> number){...}。您可以将值初始化为 -1 以指示错误:

int menuChoice;
for(;;)
{
    menuChoice = -1;
    cin >> menuChoice;
    cout << menuChoice;
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    if(menuChoice == 1 || menuChoice == 2)
    {
        cout << menuChoice << "\n";
        break;
    }
    cout << "You did not enter a valid menu option. Please try again." << endl;
}

只要确保您使用的是正确的输入即可。对于是或否选项,输入应为 char:

cout << "enter y or n\n";
for(;;)
{
    char val;
    cin >> val;
    if(val != 'y' && val != 'n')
    {
        cout << "You did not enter either y or n. Please try again." << endl;
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    else
        break;
}