0

ユーザーが選択したゲームの名前を取得して、ベクターに保存しようとしています。ユーザーがスペースを使用できるように、getline を使用します。追加する新しいゲームを入力しようとすると、入力できません。ゲームライブラリを自動的に表示します。私が間違っていることを教えてください。問題は if(action == "add") にあります

これが私のコードです:

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

using namespace std;

int main()
{
     vector<string>::const_iterator myIterator;
vector<string>::const_iterator iter;                

vector<string> games;                                
games.push_back("Crysis 2");
games.push_back("GodOfWar 3");
games.push_back("FIFA 12");

cout <<"Welcome to your Games Library.\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.\n-Type 'show' if you want to view your library.";

while (action != "exit")
{


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

              //problem is here
    if (action == "add")
    {
        cout <<"\nType the name of the game you want to add: ";
        getline (cin, newGame);

        games.push_back(newGame);

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

        continue;
    }
    else if (action == "show")
    {
        cout <<"\nThese are your games:\n";
        for (iter = games.begin(); iter != games.end(); ++iter)
        {
            cout <<*iter <<endl;
        }
    }
    else if (action == "delete")
    {
        cout <<"Type the name of the game you want to delete: ";
        cin >> newGame;
        getline (cin, newGame);

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

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

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

}
4

4 に答える 4

1

あなたが正確に書いたことはわかりませんが、

  • getlineあなたの場合、2番目のパラメーターで行全体をフェッチしますnewGame
  • cin >> newGame;getline の上で呼び出すときは、最初にistream operator >>of を使用しますstring。最初のセパレーターまで読み取ります。Tombあなたの場合、これはとの間のスペースですRaider
  • cin >> newGame;その後、 を使用して読み取った値を上書きしますgetlinegetlineその値まではTomb、あとがきになるRaider

を削除するだけcinです:

cin >> newGame;
getline (cin, newGame);

->

getline (cin, newGame);

編集今、あなたがすべてのコードを投稿したので、あなたのケースはまさに私が考えたものであると確信しています。この行cin >> action;では、ユーザーにアクションを選択するように求めます。彼はそれに入り、エンターを押して、あなたのプログラムをもう一度アクティブにできるようにします。ただし、cin >>値は読み取られますが、ユーザーが押した入力はクリアされません。したがって、最初getlineに呼び出すと、この入力が読み取られ、他には何も読み取られません。もしあなたがそうするなら:

cin >> newGame;
getline (cin, newGame);

->

cin.get();
getline (cin, newGame);

これは機能します。私はそれを試してみました。基本的に、最初cin.get();は入力をクリアしgetline、ユーザーに入力を求めます。

EDIT2後続の新しい行が処理されるレイにもう 1 つの改善を追加します。これはおそらくそれらを処理する最も正しい方法ですが、OP を複雑なコードと混同しないように、意図的にこのソリューションを提供しませんでした。

   if (cin.peek() == '\n' || cin.peek() == '\r') {
      cin.get();
   }
   getline (cin, newGame);
于 2012-04-07T11:01:20.433 に答える
1

混合

cin >> value1;

getline(cin, value2);

トラブルを招いています。問題はgetline、次の '\n' まで読み取る (そしてそれを消費する) のに対し>>、次の空白まで読み取る (そしてそれを消費しない) ことです。

これは、value1経由>>で読み取る場合、改行文字がストリームに残り、行全体を読み取ろうとして「何も」読み取ろうとすることを意味します ( はgetline、ストリームからの直接の入力である改行文字を消費します)。

を 2 倍にするという提案は機能しますが、別のブランチでgetlinevia を読み取るまでは機能しません。それは改行を消費するためです。getlinegetline>>

さらに処理が必要な場合は、を介してすべての入力を処理しgetline、読み取った文字列をトークン化することをお勧めします。このようにして、改行処理が一貫しており、スペースが正しく読み取られることも保証されます。

于 2012-04-07T11:25:15.317 に答える
0

これを試してください:

cout <<"Type the name of the game you want to add: ";

このコードの代わりに:

cout <<"\nType the name of the game you want to add: ";

うまくいくかどうかはわかりません。しかし、試してみてください。

于 2012-04-07T11:12:11.453 に答える
0

この問題は、'\n' が原因である可能性があります。名前が示すように、getline は行全体の値を取得します。コンパイラは、終了したことをどのように認識しますか。キャラクターを使用しています\n。残りを読み取る\nと、行が終了したと見なされるため、それを削除すると機能すると思います.

于 2012-04-07T11:22:05.673 に答える