我有我的C++代码的问题,我还没有真正发现任何东西在网上为什么我遇到这个问题描述。这里是我的代码:字符串超过800个字符会导致无限循环C++
/*
Write a program using vectors and iterators that allows a user to main-
tain a list of his or her favorite games. The program should allow the
user to list all game titles, add a game title, and remove a game title.
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<string> gamesList;
gamesList.reserve(10);
vector<string>::const_iterator iter;
string menu = "1. List all games\n";
menu += "2. Add a game title\n";
menu += "3. Remove a game title\n";
menu += "4. Quit\n";
string newTitle = "", removeTitle = "";
int choice = 0;
while (choice != 4)
{
cout << menu;
cout << "\nYour choice: ";
cin >> choice;
switch (choice)
{
case 1:
for (iter = gamesList.begin(); iter != gamesList.end(); ++iter)
{
cout << *iter << endl;
}
cout << "\nList capacity is " << gamesList.capacity() << endl;
break;
case 2:
cout << "Please enter a game title :";
cin >> newTitle;
gamesList.push_back(newTitle);
break;
case 3:
cout << "Which game title do you want to remove?\n";
cin >> removeTitle;
for (int i = 0; i < gamesList.size(); ++i)
{
if (gamesList[i] == removeTitle)
{
gamesList.erase(gamesList.begin() + i);
}
}
break;
case 4:
cout << "Good bye!";
break;
}
}
return 0;
}
如果我运行该程序,并输入傍,突破和俄罗斯方块到列表中,它工作正常。如果我运行该程序并输入Half Life或超过8个字符的标题,程序将进入无限循环。任何帮助将不胜感激。
你确定这是字符的数量?问题可能不在于包含空格的名称? –
你将需要[this](https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces),因此你还需要[this](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – NathanOliver
我现在明白了。谢谢你们俩。这些链接将非常有帮助。 – user10001110101