2016-08-16 8 views
-3

基本的に私はゲームのリストを作成しようとしており、ユーザに削除するオプションを与えようとしています。ユーザーが入力できる
1)チームフォートレス2
2)Skyrimの
3)ポータル

:私はリストを表示するときC++ベクタ番号にアクセスする

#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 

int main() 
{ 
//setup 
vector<string> games; 
games.push_back("Team Fortress 2"); 
games.push_back("Skyrim"); 
games.push_back("Portal"); 
vector<string>::iterator myIterator 

//main 
int delItem; 
cin >> delItem; // Consumes a number to represent the game to be deleted 

while (iter != games.end()) // Displays all the games 
{ 
    cout << j << ") " << *iter << endl; 
    iter++; 
    j++; 
} 

while ((delItem <= games.begin()) || (delItem > games.end())) // can only be 1, 2, or 3 
{ 
    cout << "\nThat input is incorrect. Please try again: "; 
    getline (cin, delItem); 
} 
myIterator = (games.begin() + (delItem - 1); 
games.erase(myIterator); // Deletes the item 
cout << "\nThe game has been deleted."; 

return 0; 
} 

だから、それは次のようになります。それは次のようになりますゲームの前にある番号で、それを削除するために選択されます。私がしようとしているのは、ユーザーがこれらの3つの数字よりも高いまたは低い数字を入力しないようにすることです。私は2番目のwhileループでこれを試みますが、games.begin()は数字ではありません。私はこれがたぶん間に合わなかったばかばかしい間違いであることを知っていますが、どんな助けも素晴らしいでしょう。

+0

は() '最大エントリの' games.sizeをお試しください:

この代わりのようにもっと何かを試してみてください。あなたは最小限のエントリが何であるかを理解できますか? –

+0

このような問題を解決する適切なツールは、デバッガを使用することですが、そうする前にStack Overflowで尋ねることはありません。 1つ目の行でコードを検査するときに行ったすべての観察を教えてください。また、[**小さなプログラムをデバッグする方法(Eric Lippertによる)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)**]を読んでみてください。あなたの問題を再現する** [MCVE] **を私たちに任せてください。 (これはπάνταῥεῖ™が提供する個人的な株式コメントです) –

答えて

0

コードにはいくつかの問題があります。

ユーザーが選択できるオプションを印刷する前に、ユーザーの入力内容を確認しています。これらの操作を元に戻す必要があります。

宣言されていない変数を使用しています。ユーザーが無効な数値を入力すると

、あなたはstd::stringに、ではないintに新しい番号が、std::getline()出力を得るためにstd::getline()を呼び出しています。

#include <iostream> 
#include <string> 
#include <vector> 
#include <limits> 

using namespace std; 

int main() 
{ 
    //setup 
    vector<string> games; 
    games.push_back("Team Fortress 2"); 
    games.push_back("Skyrim"); 
    games.push_back("Portal"); 
    vector<string>::iterator myIterator; 

    //main 

    int j = 1; 
    for (myIterator = games.begin(); myIterator != games.end(); ++myIterator) // Displays all the games 
    { 
     cout << j << ") " << *myIterator << endl; 
     ++j; 
    } 

    cout << "\nPick an item to delete: "; 

    int delItem; 
    do 
    { 
     if (cin >> delItem) // Consumes a number to represent the game to be deleted 
     { 
      if ((delItem >= 1) && (delItem <= games.size())) // can only be 1, 2, or 3 
       break; 
     } 

     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     cin.clear(); 

     cout << "\nThat input is incorrect. Please try again: "; 
    } 
    while (true); 

    /* 
    alternatively: 

    int delItem; 
    do 
    { 
     string line; 
     if (!getline(cin, line)) { 
      // error! 
      return 0; 
     } 

     istringstream iss(line); 
     if (iss >> delItem) // Consumes a number to represent the game to be deleted 
     { 
      if ((delItem >= 1) && (delItem <= games.size())) // can only be 1, 2, or 3 
       break; 
     } 

     cout << "\nThat input is incorrect. Please try again: "; 
    } 
    while (true); 
    */ 

    myIterator = games.begin() + (delItem - 1); 
    games.erase(myIterator); // Deletes the item 
    cout << "\nThe game has been deleted."; 

    return 0; 
} 
0

あなたは正しく、games.begin()は数字ではありません。それはiteratorです。ただし、games.size()を使用してgamesベクトルの要素数を判断できます。これは、ユーザーが入力できる最大数に使用できます。私が言うことから、最小数は常に同じになります。私はそれが何であるかを決定するための読者のための練習としてそれを残します。