计数器将项目添加到数组中,并剩余
Counter adding items to array with remainder
我正在创建一家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();
相关文章:
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- 为什么 C++ 中的字符数组返回其项目而不是地址?
- 数组中无法解释的最后一个项目
- 数组 C++ 中销售最多和最少的项目
- 指向单个项目或数组的指针
- 如何从类中返回自定义数组项目并操纵其属性?C
- 计数器将项目添加到数组中,并剩余
- 如何将类的项目添加到C 的数组中,而无需给它们命名
- 适当的JNI转换将数组项目从jobjectarray到分开const char
- C++检查项目是否在数组中
- C,如何将多维数组传递给CLR/类库项目中的函数
- 将项目插入数组
- C++从指针数组中删除项目
- 根据世界位置查找一维数组中的通用项目
- 在C++中初始化数组时出现 Seg 错误(欧拉项目 3)
- 对包含三个项目的数组进行排序
- 这是一个具有基于数组的列表函数的数据结构项目,我似乎无法摆脱这个无限循环。
- 如何在C++中创建一个返回字节数组的函数?Arduino项目
- 基类的指针数组;每次项目启动我都有不同的结果
- 从庸医(队列/堆栈)中包含的圆形数组顶部弹出一个项目