[英]Appending a string to a string array while validating the string. c++
我遇到的問題是提示用戶輸入控制台窗口上出現的任何項目,然后我必須檢查該字符串是否有效。 如果有效,則必須附加該字符串,顯示玩家清單中顯示的所有項目,並相應扣除玩家的資金。
問題始於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');
照顧那個。 您需要在幾個地方這樣做。
關於將輸入追加到玩家的清單。 您可以使用以下內容:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.