簡體   English   中英

getline 不讓我輸入,c++

[英]getline won't let me type, c++

我嘗試獲取用戶選擇的游戲名稱並將其存儲在向量中。 我使用 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;

}

我不明白你到底寫了什么,但是:

  • getline將在您的案例newGame的第二個參數中獲取整行
  • 當你調用cin >> newGame; 在 getline 之上,您首先使用istream operator >> of string 它一直讀到第一個分隔符。 在您的情況下,這是TombRaider之間的空間
  • 之后你用cin >> newGame; 使用getline 直到getline的值是Tomb ,之后它變成了Raider

只需刪除cin

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

->

getline (cin, newGame);

編輯現在,當您發布所有代碼時,我確信您的情況正是我所想的。 cin >> action; 你提示用戶 select 一個動作。 他輸入它並按回車鍵,這樣他就可以再次激活您的程序。 但是, cin >>將讀取該值,但不會清除用戶按下的輸入。 因此,您調用的第一個getline將只讀取此輸入,而不會讀取其他任何內容。 如果你這樣做:

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

->

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

這會起作用。 我嘗試過這個。 基本上是第一個cin.get(); 清除輸入, getline提示用戶輸入。

EDIT2在 lay 上添加了一個改進,處理了尾隨的新行。 這可能是處理它們的最正確方法,但我故意沒有提供此解決方案,以免將 OP 與復雜代碼混淆:

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

混合

cin >> value1;

getline(cin, value2);

是自找麻煩。 問題是getline讀取到下一個 '\n'(並消耗它),而>>讀取到下一個空白(並且不消耗它)。

這意味着當您通過>>讀取value1時,換行符保留在 stream 中,然后您嘗試讀取整行並讀取“無”( 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 如果它讀取一個 left over \n它將假定該行已經結束,所以如果你刪除它我認為它應該工作。所以只需找出 getline 之前的行可能已經離開了一個流浪 \n。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM