c++ - 在验证字符串时将字符串 append 到字符串数组。 C++

标签 c++ arrays string append

我遇到的问题是提示用户输入出现在控制台窗口上的任何项目,然后我必须检查该字符串是否有效。如果有效,我必须 append 该字符串,显示出现在玩家库存中的所有元素并相应地扣除玩家的钱。

问题始于 playerInput 函数。

#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <algorithm>

using namespace std;

const int items = 5;
const string ShopInventory[items] = {"Helms", "Boots", "Swords", "Axes", "Leather Armors"};
const int ItemPrices[items]= {10, 5, 20, 30, 50};

void PrintShop();
int PlayerInput(string [], int&);

int main()
{
   string PlayerInventory[items];
   int playerMoney = 100;
   char Response;

   cout << "\t\tWelcome traveler to my lovely shop. \n";

   cout << "\nFeel free to browse many of the wonderful items within";
   cout << " this store. " << endl;

   PrintShop();

   while(toupper(Response) != 'N')
   {
      cout << PlayerInput(PlayerInventory, playerMoney);

      cout << "\nIs there anything else that you would like to buy? ";
      cout << "Enter n or N to quit.\nElse enter y or Y to continue. ";
      cin >> Response;

      while(toupper(Response) != 'Y' && toupper(Response) != 'N')
      {
         cerr << "\nSorry, wrong input. Please try again.  ";
         cin >> Response;
      }
   }

   return 0;
}

void PrintShop()
{
   cout << endl;

   cout << "     ***** Shop Inventory ***** " << endl << endl;

   cout << "Shop items " << "\t\tPrice Per Item. " << endl << endl;

   for(int i=0; i<items; i++)
   {
      cout << i+1 << ".) " << left << setw(22) << ShopInventory[i];
      cout << left << setw(3) << ItemPrices[i] << " Gold" << endl;
   }
}

int PlayerInput(string PlayerInventory[], int &playerMoney)
{
   string input;

   cout << "\nEnter what you would like to buy. ";
   getline(cin, input);

   while(find(begin(ShopInventory), end(ShopInventory), input) == end(ShopInventory))
   {
      cout << "\nThe item that you enter isn't in my inventory. ";
      getline(cin, input);
   }

   return playerMoney;
}

最佳答案

您阅读 y/Y/n/N 的方式有问题响应。

行后

  cin >> Response;

执行后,换行符仍留在输入流中。

将其更改为:

  cin >> Response;
  cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

来处理这个问题。您需要在几个地方执行此操作。

关于将输入 append 到玩家的元素栏。您可以使用以下内容:

int PlayerInput(string PlayerInventory[], int& playerMoney)
{
   string input;

   while (true)
   {
      cout << "\nEnter what you would like to buy. ";
      getline(cin, input);

      auto it = find(begin(ShopInventory), end(ShopInventory), input);
      if ( it == end(ShopInventory))
      {
         cout << "\nThe item that you enter isn't in my inventory. ";
         continue;
      }

      int index = std::distance(begin(ShopInventory), it);

      // Use the knowledge that the default constructor of std::strings
      // constructs an empty string.
      if (!PlayerInventory[index].empty() )
      {
         cout << "\nThe item that you enter is already in your inventory. ";
         continue;
      }
      else
      {
         // Add the item to the player's inventory.
         PlayerInventory[index] = input;

         // Subtract the item's price from the player's available money
         playerMoney -= ItemPrices[index];

         break;
      }
   } 

   return playerMoney;
}

更好的选择是使用 std::set<std::string>代表玩家的元素栏。在这种情况下,您的功能将是:

int PlayerInput(set<string>& PlayerInventory, int& playerMoney)
{
   string input;

   while (true)
   {
      cout << "\nEnter what you would like to buy. ";
      getline(cin, input);

      auto it = find(begin(ShopInventory), end(ShopInventory), input);
      if ( it == end(ShopInventory))
      {
         cout << "\nThe item that you enter isn't in my inventory. ";
         continue;
      }

      int index = std::distance(begin(ShopInventory), it);

      if (PlayerInventory.find(input) != PlayerInventory.end())
      {
         cout << "\nThe item that you enter is already in your inventory. ";
         continue;
      }
      else
      {
         // Add the item to the player's inventory.
         PlayerInventory.insert(input);

         // Subtract the item's price from the player's available money
         playerMoney -= ItemPrices[index];

         break;
      }
   } 

   return playerMoney;
}

关于c++ - 在验证字符串时将字符串 append 到字符串数组。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27476337/

相关文章:

string - 运行时构建 : String not found in this scope

c++ - boost 过滤适配器编译

c++ - 使用优先级队列 C++ 将霍夫曼树算法从 O(n^2) 优化到 O(n)

c++ - 如何按降序打印数组?

arrays - 如果可以删除任何一个元素,则查找是否可以将数组分为相等和的两个子数组

arrays - 如何在 Go 中将错误数组转换为 JSON

javascript - 如何从javascript中的构造函数返回字符串?

c++ - 不可行函数模板的类型推导

php - SimpleXMLElement 和警告 : Illegal offset type

将字符串转换为特殊字符串