2017-06-02 53 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中删除一个新的行字符。