Getline 不让我输入,C++

getline won't let me type, c++

本文关键字:C++ 输入 Getline      更新时间:2023-10-16

我试图获得用户选择的游戏名称,并将其存储在向量中。我使用getline以便用户可以使用空间。当我尝试键入一个新游戏来添加时,它不允许我。它自动显示我的游戏库。请告诉我我做错了什么。问题出现在如果(操作=="添加")

这是我的代码:

#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 <<"nnWhat 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;
}

我不知道你到底写了什么,但:

  • getline将在您的案例newGame中的第二个参数中获取整行
  • 当您在getline上方调用cin >> newGame;时,您必须使用stringistream operator >>。它一直读到第一个分隔符。在您的情况下,这是TombRaider之间的空间
  • 使用getline覆盖用cin >> newGame;读取的值。在getline之前,其值为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在布局上再添加一个改进,即可处理后面的新行。这可能是处理它们的最正确的方法,但我故意没有提供这个解决方案,试图不将OP与复杂代码混淆:

   if (cin.peek() == 'n' || cin.peek() == 'r') {
      cin.get();
   }
   getline (cin, newGame);

混合

cin >> value1;

getline(cin, value2);

正在招致麻烦。问题是getline读取到下一个"\n"(并消耗它),而>>读取到下个空白(并不消耗它)。

这意味着,当您通过>>读取value1时,换行符仍保留在流中,然后您尝试读取整行并读取"nothing"(getline将使用作为流的即时输入的换行符)。

getline加倍的建议将起作用,但只有当您通过另一个分支中的getline读取时,它才会断开,因为getline会消耗新行,而>>不会。

如果需要进一步处理,我建议您通过getline处理所有输入,然后标记读取的字符串。这样可以确保新行处理是一致的,并且可以正确读取空格。

你能试试这个吗:

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

而不是你的这个代码:

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

我不确定它是否有效。但请试一试。

问题可能是由于一个杂散'\n'。顾名思义,getline获取整行的值。那么你的编译器是如何知道你什么时候完成的呢。它使用字符n。如果它读取到一个遗留的n,它将假设该行已经结束,所以如果你删除它,我认为它应该可以工作。所以,只要找出getline之前的行,它可能留下了一个杂散。