2016-08-01 4 views
-3

私が作成しているC++アプリケーションでは、お気に入りのゲームをリストに追加したり削除したりすることができます。私のお気に入りのリストを作成する

リストを注文したいと思います。

  1. 私は初心者ですゼルダの伝説
  2. コントラ
  3. GTA V私はしかし、私は、ベクター(複数可)を使用しています、forループ/しばらく使用する必要があります知っている

私のコードでwhile/forループをどこに置くべきかはわかりません。私がこれまで試してみました何

は次のとおりです。

失敗した1 - ループの入れ子になった:

for (gameInter = list.begin(); gameInter != list.end(); gameInter++) { 
     for (int listNum = 1; listNum < list.size(); listNum++) { 
      cout << listNum << ". " << *gameInter << endl; 
     } 
    } 

失敗した2 - A whileループ:

case 1: 
     while(listNum < list.size()) { 
      cout << "Type the game title to add: \n"; 
      cin.get(); 
      getline(cin, addGame); 
       list.push_back(addGame); 
       sort(list.begin(), list.end()); 
       cin.clear(); 
       cin.sync(); 
       cout << "\nYour game was successfully added.\n"; 
       listNum++; 
     } 
      break; 

完全なコード:

#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <string> 

using namespace std; 

int main() 
{ 

    int userSelection = 0; 
    int listNum = 1; 

    string addGame, removeGame; 

    vector<string> list; 
    vector<string>::iterator gameInter; 

    while (userSelection != 4) { 

     cout << "Type 1 to add a game to your list\n"; 
     cout << "Type 2 to remove a game from your list\n"; 
     cout << "Type 3 to list all games\n"; 
     cout << "Type 4 to quit\n"; 

     cin >> userSelection; 

     switch (userSelection) { 
     case 1: 
      cout << "Type the game title to add: \n"; 
      cin.get(); 
      getline(cin, addGame); 
       list.push_back(addGame); 
       sort(list.begin(), list.end()); 
       cin.clear(); 
       cin.sync(); 
       cout << "\nYour game was successfully added.\n"; 
      break; 
     case 2: 
      cout << "Type the game title to remove: \n"; 
      cin.get(); 
      getline(cin, removeGame); 
      gameInter = find(list.begin(), list.end(), removeGame); 
      if (*gameInter == removeGame) { 
       cout << "Title " << removeGame << " found\n"; 
       list.erase(gameInter); 
       cout << "Title " << removeGame << " has been removed!\n"; 
      } 
      else 
      { 
       cout << "Title " << removeGame << " cannot be found!\n"; 
      } 
      break; 
     case 3: 
      cout << "\nYour Favorite Games Are: \n"; 

       for (gameInter = list.begin(); gameInter != list.end(); gameInter++) { 
        cout << listNum << ". " << *gameInter << endl; 
      } 
      break; 
     case 4: 
      cout << "Thank You for your input! Goodbye\n"; 
      //userSelection = 4; 
      break; 
     default: 
      cout << "That is not a valid option\n"; 
      break; 
     } 
    } 
    system("pause"); 
    return false; 
} 
+3

1)これは、グローバルスコープで 'using namespace std'を使うべきではない理由です:' vector list; '。私。変数にSTL型を指定しないでください。 2)遭遇するエラーは何ですか? –

+0

ダウン投票:コメントしてください! –

+0

コメント#1、 "失敗した試行" - _what_が失敗しましたか? 「それは機能していません」と言うだけで問題の説明がない解決策を期待することはできません。そのアプローチの人気は私を驚かせることを止めることは決してありません:人々はそれがどのように働くと思いますか?とにかく、 'std :: set >'のように、stdlibが正確にこのようなケースのための素晴らしい機能を提供するときには、これは 'pair :: first'のランクに従って自動的に順序付けされます(もしあれば、' pair :: second'の中に名前があります)。 –

答えて

0

あなたの生活を楽にするために、標準ライブラリが提供する特殊な容器を使用することを検討してください。彼らはあなたが苦労して車輪を再発明しなくて済むように存在するので、あなたは何をする必要があるかに焦点を当てることができます。私を間違ってはいけない:vectorを使うと、あなた自身の配列を手作業で割り当てるのではなく、多くを先取りすることができます。

例えば、自動的に自分のランクでソートされますランクとタイトルを持つゲームのグループは、(とネクタイが発生した場合、そのタイトル)を作成するには:

#include <iostream> 
#include <set> 
#include <string> 
#include <tuple> // pair 

// Represent a game by its rank and title (typedef) 
using game_type = std::pair<unsigned, std::string>; 

// Create an ordered set of games 
std::set<game_type> games; 
// Populate it. You could do this from standard input. 
games.emplace(3, "GTA V"); // constructs element in-place 
games.emplace(1, "Legend Of Zelda"); 
games.emplace(2, "Contra"); 

// Verify that std::pair sorts by .first (and then .second) 
for (auto const &it: games) { // tidy C++11 iteration syntax 
    std::cout << it.first << ": " << it.second << std::endl; 
} 

を出力は何です欲しい:

1: Legend of Zelda 
2: Contra 
3: GTA V 

ではなく、ユーザの入力からmapを投入したいですか?確かに。ランクとタイトルをテンポラリ変数にして、emplace(theRank, theTitle)としてください。

次に、ランクに基づいて消去する方法について考え始め、std::setのドキュメントまたは<algorithm>で回答を見つけることができます。私は既にあなたがすでに後者を見てきたのを見ます。それは素晴らしいことです!私たちを助けるためにそこにあるが、時々見落とされる。または、人々はその機能を使用しますが、#includeはありません。それ以外の間接的な包含に依存しています...

とにかく。たぶんあなたはランクが一意であることを望み/必要とし、ユーザーが複製を入力する状況を捉える必要があるかもしれません。その後、std::mapをチェックしてください。

など。標準ライブラリには非常に多くの可能性があります。あなたのことを忘れてしまって、無意識に頭を痛めて自分自身を改革しようとするのは残念です。 ;-)

関連する問題