c++ - 变量变化值和不确定原因
问题描述
所以我对 c++ 和一般编程还是很陌生,但我想假设我对概念和控制流有相当好的掌握(可能是大声笑)。我目前正在创建一个自上而下的游戏,允许用户与箱子的库存进行交互。让我非常困惑的问题是我修改了一个变量,但是在“更新”控制台之后它变成了完全不同的东西。 按 s 更新控制台后显示 10 金币的交互式胸部库存
在 chestLoot() 函数中,宝箱中的金币数量设置为 10,但随后更改为 303,这是治疗药水的宏。为什么我设置为 10 后,chest.gold 值变为 303?对此的任何帮助将不胜感激。(不要太苛刻,我对堆栈溢出和 c++ 以及一般编程还是新手)
这是处理胸部库存以及生成的战利品的代码:
void chestLoot(int x)
{
switch (tutorial.chestIt)
{
case 0:
chest.inventory[x] = WOODEN_SWORD_ID;
break;
case 1:
chest.inventory[x] = GOLD_ID;
chest.gold = 10;
break;
case 2:
chest.inventory[x] = HEALING_POTION_ID;
break;
case 3:
chest.inventory[x] = LEATHER_HELMET_ID;
break;
}
tutorial.chestIt++;
}
void chestInventory()
{
bool endLoop = false;
int x = 0;
int selection = 0;
int highlighted = 0;
while (endLoop == false)
{
system("cls");
std::cout << "Chest:" << std::endl << std::endl;
if (tutorial.gridChestOpened == false)
updatePlayer(5);
for (x = 0; x <= player.level + 3; x++)
{
if (tutorial.gridChestOpened == false)
{
chestLoot(x);
}
switch (chest.inventory[x])
{
case WOODEN_SWORD_ID:
std::cout << x + 1 << ". ";
if (selection == x)
{
std::cout << "- ";
highlighted = selection;
}
std::cout << "Wooden Sword";
break;
case GOLD_ID:
std::cout << x + 1 << ". ";
if (selection == x)
{
std::cout << "- ";
highlighted = selection;
}
std::cout << chest.gold << " Gold";
break;
case HEALING_POTION_ID:
std::cout << x + 1 << ". ";
if (selection == x)
{
std::cout << "- ";
highlighted = selection;
}
std::cout << "Healing Potion";
break;
case LEATHER_HELMET_ID:
std::cout << x + 1 << ". ";
if (selection == x)
{
std::cout << "- ";
highlighted = selection;
}
std::cout << "Leather Helmet";
break;
case NULL:
break;
}
std::cout << std::endl;
}
tutorial.gridChestOpened = true;
switch (_getch())
{
case 'w':
--selection;
if (selection < 0)
selection = 0;
if (chest.inventory[selection] == NULL)
++selection;
break;
case 's':
++selection;
if (selection > tutorial.chestIt)
selection = tutorial.chestIt;
if (chest.inventory[selection] == NULL)
--selection;
break;
case 'e':
if (chest.inventory[highlighted] == 0)
{
std::cout << "There is nothing there";
break;
}
else if (chest.inventory[highlighted] == GOLD_ID)
{
player.gold = player.gold + chest.gold;
chest.inventory[highlighted] = NULL;
break;
}
else
{
for (int y = 1; y <= player.level + 9; y++)
{
if (player.inventory[y] == NULL)
{
player.inventory[y] = chest.inventory[highlighted];
chest.inventory[highlighted] = NULL;
}
}
}
break;
case 27:
endLoop = true;
drawScreen(NULL);
break;
}
}
}
以下是函数中使用的全局变量:
struct Loot
{
int inventory[2];
int gold;
}chest, pot;
struct Entity
{
int position[2] = { 2, 2 };
int gold = 0;
int health = 10;
int level = 1;
int totalXp = 0;
int inventory[11];
int mainHand[2] = {/*location, item*/};
}player/*enemy*/;
struct Grid
{
int grid[6][12] = {
{1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1,1,1,1,1},
{1,0,0,0,0,2,0,1,0,0,3,1},
{1,0,2,0,0,0,0,1,0,0,0,1},
{1,0,0,0,0,2,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,4,1}
};
bool gridChestOpened = false;
int chestIt = 0;
}tutorial;
// Item ID's
#define WOODEN_SWORD_ID 301
#define GOLD_ID 302
#define HEALING_POTION_ID 303
#define LEATHER_HELMET_ID 304
任何关于改进我的代码的提示也非常感谢:)
2 : Check Here将变量从一个函数传递到另一个函数
解决方案
好吧,您的“战利品”结构的库存成员是一个长度为 2 的整数数组。但是您似乎正在调用值大于 1 的“chestLoot”。这导致您离开数组的末尾并编写到“黄金”应该在的地方。
推荐阅读
- netsuite - 将销售订单转换为发票 Netsuite
- visual-studio - 如何将 .Net Core 2.1 项目更改为 .Net Core 2.0 项目
- reactjs - React/Redux - 新道具 - 未调用 componentDidUpdate
- azure - U-SQL 错误 - 更改标识符以使用至少一个小写字母
- macos - 基于 REST 的 Cryptotokenkit 实现
- android - 如何从另一个类 React Native 更改 navigationOptions
- java - 无法压缩位图android studio
- javascript - discord.js-commando if else 循环
- google-app-engine - 将防火墙规则应用于谷歌云中的特定标签不起作用
- c# - 如何根据 WebAPI 中的参数绑定模型