2017-06-02 54 views
0

我有我的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個字符的標題,程序將進入無限循環。任何幫助將不勝感激。

+1

你確定這是字符的數量?問題可能不在於包含空格的名稱? –

+2

你將需要[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

+0

我現在明白了。謝謝你們倆。這些鏈接將非常有幫助。 – user10001110101

回答

2

問題不在於長度,而是您嘗試在其中輸入空格的名稱。輸入操作員>>分隔空間。因此,如果您輸入Half Life作爲名稱,輸入運算符將只會讀取Half

你或許應該使用std::getline相反,讀的名字。

至於無限循環,這是因爲由於名稱的一部分仍然在輸入緩衝區中(具有前導空格),因此當您嘗試讀取菜單項的數字時,輸入將失敗,離開輸入在緩衝區中,你不會檢測到它,並進入一個無限循環,你想要讀取一個整數,失敗和打開,然後...

使用std::getline將解決這兩個問題。但是如果你想確保這不會再發生,你必須在讀取菜單選項的整數時添加一些錯誤檢查。這可以簡單地像

while (!(cin >> choice)) 
{ 
    // Input of menu alternative failed, ignore input until the end of the line 
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
} 

An std::istream::ignore reference

+0

好的,謝謝。我不知道>>表現如此。 – user10001110101

+0

@ user10001110101閱讀(和*理解*)[文檔](http://en.cppreference.com/w/cpp/io/basic_istream)您使用的功能通常是一個好主意...... –

0

對於初學者來說,你應該包括頭<string>因爲有從程序標題中使用的聲明。

#include <string> 

這從矢量

 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); 
       } 
      } 

移除元素的代碼片段是錯誤的。

首先根據你不得不刪除只有一個元素的分配。 要刪除一個元素,你可以使用只有一個聲明,沒有任何環

#include <algorithm> 

//... 

gamesList.erase(std::find(gamesList.begin(), gamesList.end(), removeTitle)); 

至於你的問題那麼oprator >>輸入字符,直到一個空白字符遇到。您應該使用功能getline而不是操作員。考慮到你還需要使用成員函數ignore從輸入bu = ffer中刪除一個新的行字符。