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

时间:2017-11-15 19:10:32

标签: c++ validation input menu getline

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

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,它将通过fight函数运行。但是,如果我经历了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();
    }

如果有人可以指出我在获取用户输入时出错的地方,我会很感激,因为我已经没有尝试我所知道的事情了。

1 个答案:

答案 0 :(得分:0)

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

创建了许多问题

>>运算符会将输入字符串"1."转换为1,如果您在ignore(...,'\n')之前调用.\n之前的任何其他字符将被忽略。 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;
}