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

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

本文关键字:输入 下一个 溢出 getline 字符串 程序 使用      更新时间:2023-10-16

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

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,它将成功地从主程序中退出,或者如果输入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'),则.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 nn";
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;
}