计数器将项目添加到数组中,并剩余

Counter adding items to array with remainder

本文关键字:数组 项目 添加 计数器      更新时间:2023-10-16

我正在创建一家RPG商店。它必须有商品,黄金和物品价格。本质上创建库存。我要实现的是,球员黄金是0,他们不能再在库存中添加任何物品,并且不能具有负金。

在调试模式下运行我的代码时,它似乎正在执行我想要的操作,但是当功能退出时,播放器所请求的金额尚未反驳。

请记住,我仍然是C 的新手。

谢谢

#include <iostream>
#include <string>
using namespace std;
// Global consts
const int numItems = 4;
const string items[numItems] = {"boots", "hats", "cats", "bats"}; // create string array of numItems items.

// Create stuct, that holds:
// Item, gold, price.
struct Inv {
    int pInv[numItems] = {0, 0, 0, 0};
    int gold = 100;
    int itemPrice[numItems] = { 10, 6, 12, 15 };
}inv;
void iniItems();
void printItems();
bool buyItems();
bool sellItems();
int main() {
    bool isDone = false;
    iniItems();
    while (isDone == false) {
        printItems();
        int choice;
        bool x = false;
        cout << "nWhat would you like to do? Enter (" << 1 << "-" << 2 << "): " << endl;
        cout << "1: Buy Items. n2: Sell Items." << endl; cin >> choice; cout << endl;
        while (x == false) {
            if (choice == 1) {
                x = buyItems();
            }
            if (choice == 2) {
                x = sellItems();
            }
        }
    }

    system("pause");
    // dynamic memory not implemented yet. Must wait for working fix of shoppe.cpp
}
void iniItems() {
    cout << "** Shop Inventory: **" << endl;
    for (int i = 0; i < numItems; i++) {
        cout << i + 1 << " - " << items[i] << " - price: $" << inv.itemPrice[i] << endl;
    }
}
void printItems() {
    cout << "n** Player Inventory: **" << endl;
    cout << "Gold: $" << inv.gold << endl;
    for (int i = 0; i < numItems; i++) {
        cout << inv.pInv[i] << " x " << items[i] << endl;
    }
}
bool buyItems() {
    bool exit = false;
    int amount;
    const int remainder = 10;
    printItems();
    cout << "nEnter -1 to quit." << endl;
    cout << "What would you like to buy? Enter (" << 1 << "-" << 4 << "): " << endl;
    // Get item info.
    while (exit == false) {
        int inp;
        cout << "Item: "; cin >> inp; cout << endl;
        cout << "Amount: "; cin >> amount; cout << endl;
        // Check if input is valid.
        if (inp > 0 && inp <= numItems) {
            if (amount >= 0) {
                inv.pInv[inp - 1] = 1 * amount;
                inv.gold = inv.itemPrice[inp - 1] / amount;
            }
            // If gold is 0, make sure the user cannot gain more items.
            if (inv.gold <= 0) {
                int tmp;
                inv.gold = 0;
                tmp = remainder - amount;
                for (int i = tmp; i >= 0; i++) {
                    inv.pInv[inp - 1]--;
                }
                return inv.pInv[inp - 1];
            }
            if (inp == -1) {
                return true;
            }
            if (inp > numItems) {
                cout << "Enter valid number." << endl;
                return false;
            }
            else return false;
        }
    }
    if (exit == true) {
        return true;
    }
}

因此,我将代码限制为do to do wher the lip for the for the buyitems()函数中的counter。在这里,如果有人有兴趣。

do {
    inv.gold -= inv.itemPrice[inp - 1];
    ++(inv.pInv[inp - 1]);
} while (inv.gold > 0);
if (inv.gold < 0) {
    inv.gold = 0;
    inv.pInv[inp - 1]--;
}
printItems();