双线,当要求用户按 i 查看库存时

Double Line when user is asked to press i to view Inventory

本文关键字:用户 双线      更新时间:2023-10-16

嘿 基本上我有 2 个函数:

void Inventory:: showInventory()
{
    char input[80];
    cin >> input;
    char inventoryRequest[] = "i";
    //compare the player input to inventoryRequest (i) to see if they want to
    //look at inventory.
    int invent = strcmp (input,inventoryRequest);
    if(invent == 0) {
        //vector<string> inventory;
        cout << "You have " << inventory.size() << " items.n";
        cout << "n******Inventory******";
        cout << "nYour items:n";
        for (int i= 0; i< inventory.size(); ++i) {
            cout<< inventory[i] << endl;
        }
    }
}
void Inventory :: displayInventory(const string str) {
    char input = 0;
    do
    {
        cout << str << endl;
        cin >> input;
    }
    while((input != 'i') && (input != 'I') && (input != 'n') && (input != 'N'));
    showInventory();
    //return input;
}

showInventory 将玩家输入与 i 进行比较。显示清单仅允许用户按 I 或 N。I 查看库存,n 跳过。但是当我被按下时。 它会导致双线。

这意味着我必须按两次才能查看库存。

我已经尝试了许多方法来阻止这种情况发生。 但我没有成功,大多数时候根本无法查看库存。

任何人都可以帮我解决这个问题。提前谢谢。

尝试在 void Inventory::showInventory() 上使用参数进行输入,并消除第二个cin,如下所示:

void Inventory:: showInventory(char input)
{
    //char input[80];
    //cin >> input;
    //char inventoryRequest[] = "i";
    //int invent = strcmp (input,inventoryRequest);
    //compare the player input to inventoryRequest (i) to see if they want to look at inventory.
    //if(invent == 0)  // REPLACE THIS WITH THE LINE BELOW
    if(input == 'i')

然后当你调用它时,这样做:

    showInventory(input);