【问题标题】:Appending a string to a string array while validating the string. c++在验证字符串时将字符串附加到字符串数组。 C++
【发布时间】:2015-02-13 02:15:27
【问题描述】:

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

问题从 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;
}

【问题讨论】:

  • 你说有问题,但你只提供代码,没有说明问题是什么。
  • 我的问题是,在验证用户输入的字符串是否出现在 shopinventory 数组中时,我必须将该字符串附加到 playerinventory 数组中,如果不添加来自shopinventory 数组到 playerinventory 数组中。
  • 然后我要告诉玩家他有x数量的斧头、头盔、盔甲等。然后我必须从他的钱包中扣除x数量。
  • 程序本身没有问题。在尝试将字符串附加到字符串数组时,我刚刚碰到了一堵墙。我在描述中提到的每一件事都是这个程序必须面对的问题。一旦我将验证过的字符串附加到 playerInventory 数组中,我就可以继续处理该程序中的其他问题(即使验证字符串也很难做到)。

标签: c++ arrays string append


【解决方案1】:

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

行后

  cin >> Response;

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

将其更改为:

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

来解决这个问题。您需要在几个地方这样做。

关于将输入附加到玩家的库存中。您可以使用以下内容:

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&lt;std::string&gt; 来表示玩家的库存。在这种情况下,您的功能将是:

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;
}

【讨论】:

  • 我的程序没有吃掉我的任何输入缓冲区(据我所知)r。我的问题是尝试将字符串(输入)附加到字符串数组(playerInventory 数组)。这就是我难过的地方。
  • 很好的解释。你完美地解释了一切
  • 我唯一想知道的是你为什么使用汽车?我以前从没用过 auto 也不知道什么时候用。
  • @ChronoTrigger,当类型太复杂而无法键入时使用auto,或者在给定模板参数的情况下不容易推断。简要介绍见stroustrup.com/C++11FAQ.html#auto
猜你喜欢
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多