2012-04-06 50 views
1

我正在構建遊戲庫,並希望用戶能夠刪除遊戲。 我想使用.erase()函數,但我在某處犯了一個錯誤。 我正在學習我的代碼幾天,我找不到一個anwser。 爲什麼?因爲我只有15歲,並沒有其他程序員擁有的編程思想。這就是爲什麼我不稱自己是一名程序員,而只是一名嗜血青少年。請幫幫我。 (對不起,我的拼寫)在C++中遇到問題.erase()

這裏是我的代碼:

int main() 
{ 
    vector<string>::const_iterator myIterator; 
    vector<string>::const_iterator iter;    
              vector<string> games;         
    games.push_back("Crysis2"); 
    games.push_back("GodOfWar3"); 
    games.push_back("FIFA12"); 

    cout <<"Welcome to your Games Library.\n"; 
    cout <<"\nWarning!!! Don't type spaces, put ecerything together!!!\n"; 
    cout <<"\nThese are your games:\n"; 
    for (iter = games.begin(); iter != games.end(); ++iter) 
    { 
     cout <<*iter <<endl; 
    } 
    //the loop! 
    string action; 
    string newGame; 

    cout <<"\n-Type 'exit' if you want to quit.\n-Type 'add' if you want to add a game.\n-Type 'delete' if you want to delete a game.\n-Type 'find' if you want to search a game.\n-Type 'game' if you don't know what game to play. "; 

    while (action != "exit") 
    { 


     cout <<"\n\nWhat do you want to do: "; 
     cin >> action; 


     if (action == "add") 
     { 
      cout <<"\nType the name of the game you want to add: "; 
      cin >> newGame; 

      games.push_back(newGame); 

      for (iter = games.begin(); iter != games.end(); ++iter) 
      { 
       cout <<*iter <<endl; 
      } 

      continue; 
     } 
     else if (action == "delete") 
     { 
      cout <<"Type the name of the game you want to delete: "; 
      cin >> newGame; 

      iter = find(games.begin(), games.end(), newGame); 

      if(iter != games.end()) 
      { 
       games.erase(newGame); 
      } 
      else 
      { 
       cout<<"\nGame not found."; 
      } 

      continue; 
     } 
     else if (action == "find") 
     { 
      cout <<"Which game you want to look for in your library: "; 
      cin >> newGame; 

      iter = find(games.begin(), games.end(), newGame); 

      if (iter != games.end()) 
      { 
       cout << "Game found.\n"; 
      } 
      else 
      { 
       cout << "Game not found.\n"; 
      } 

      continue; 
     } 
     else if (action == "game") 
     { 
      srand(static_cast<unsigned int>(time(0))); 
      random_shuffle(games.begin(), games.end()); 
      cout << "\nWhy don't you play " <<games[0]; 

      continue; 
     } 
     else if (action == "quit") 
     { 
      cout <<"\nRemember to have fun while gaming!!\n"; 
      break; 
     } 
     else 
     { 
      cout <<"\nCommand not found"; 
     } 
    } 
    return 0; 
} 
+1

我認爲這將有助於如果你能包括的 '遊戲' 和 'newgame' 的定義。 – juanchopanza 2012-04-06 11:02:29

+0

我們需要看到更多的代碼。你也沒有描述你遇到的問題。它是否編譯?當你運行它會發生什麼? – jalf 2012-04-06 11:03:05

+0

你需要粘貼較大的一段代碼。我們不知道「遊戲」是什麼(矢量?列表?)。塊以「繼續」結束 - 你在那裏繼續什麼循環?你怎麼知道你在犯錯誤?你遇到了什麼問題? – Krizz 2012-04-06 11:03:36

回答

2

erase()需要一個迭代器(在這種情況下,find的結果,這是iter),而不是字符串(如newGame)。

+0

你不僅說錯了,而且爲什麼,非常感謝你! – Stijn 2012-04-06 11:16:42

0
iterator erase (iterator position); 
iterator erase (iterator first, iterator last); 

嘗試用games.erase(iter);

4

更換games.erase(newGame);你只是傳遞錯誤的參數刪除。該代碼

if(iter != games.end()) 
{ 
    games.erase(newGame); 
} 

應該

if(iter != games.end()) 
{ 
    games.erase(iter); 
} 
+0

謝謝這幫助我分配。我沒有選擇你作爲答案,因爲我看到你有很高的聲譽,@ jpm告訴我爲什麼我的代碼是錯誤的。但仍然感謝您的回答:) – Stijn 2012-04-06 11:15:56

+0

@Stijn - 那很好。這是您最喜歡的答案的選擇。你說得對,我已經有了超過需要的點數。 :-) – 2012-04-06 11:24:25