我有我的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