-1

嘿基本上我有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 跳过。但是当我被按下时。它会导致双线。

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

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

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

4

1 回答 1

1

尝试使用参数输入 on 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);
于 2012-11-29T22:02:21.107 回答