我遇到了一个对我来说毫无意义但通过使用参数解决的问题。基本上,这有效:
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(input == 'i')
{
cout<< "\nYou have " << inventory.size() << " items.\n";
cout << "----------------Inventory----------------\n";
cout<< "\nYour items:\n";
for (int i= 0; i< inventory.size(); ++i)
cout<< inventory[i] << endl;
}
cout << "\n-----------------------------------------\n\n\n";
}
而不是这样:
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(input == 'i')
{
cout<< "\nYou have " << inventory.size() << " items.\n";
cout << "----------------Inventory----------------\n";
cout<< "\nYour items:\n";
for (int i= 0; i< inventory.size(); ++i)
cout<< inventory[i] << endl;
}
cout << "\n-----------------------------------------\n\n\n";
}
基本上我认为这是一样的。但显然不是当第一个有效而第二个无效时。任何人都可以对此有所了解。