2012-04-06 3 views
1

私はゲームライブラリを構築しており、ユーザーはゲームを削除できます。 .erase()関数を使用しようとしていますが、どこかで間違いをしています。 私は数日間コードを勉強しています。アンサーを見つけることができません。 なぜですか?私はちょうど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

+0

さらにコードを見る必要があります。あなたはまた、あなたが得ている問題を記述していません。それはコンパイルされますか?あなたがそれを実行するとどうなりますか? – jalf

+0

大きなコードを貼り付ける必要があります。私たちは 'ゲーム'が何であるかを知らない(ベクトルリスト?)。ブロックは 'continue'で終わります - あなたはどこのループを続けていますか?あなたは間違いをしていることをどのように知っていますか?あなたが得た問題は何ですか? – Krizz

答えて

2

erase()は、イテレータ(iterあるfind、この場合、結果)ではなく、(newGameのような)文字列を取ります。

+0

あなたは何が間違っていたと言うだけでなく、なぜ、非常においてくれてありがとう! – Stijn

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

+0

@ Stijn - それは問題ありません。あなたが一番好きな答えはあなたの選択です。そして、あなたは正しいです、私はすでに必要以上に多くのポイントを持っています。 :-) –