可变值变化,不确定原因

Variable changing value and unsure of why

本文关键字:不确定 变化      更新时间:2023-10-16

所以我对c++和编程还很陌生,但我想假设我对概念和控制流有很好的理解(可能是lol(。我目前正在创建一个自上而下的游戏,允许用户与箱子的库存进行交互。让我非常困惑的问题是,我修改了一个变量,但在"更新"控制台后,它变成了完全不同的东西。展示10金的交互式胸脯库存按s更新控制台后

在chestLoot((函数中,箱子中的黄金数量被设置为10,但随后被更改为303,这是治疗药剂的宏。为什么在我将chest.gold值设置为10之后,它被更改为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:检查此处将变量从一个函数传递到另一个

您的问题是Loot的成员inventory的尺寸不正确。chest.invetory[x]x的唯一有效值为0和1。然而,您的代码清楚地写入了chest.inventory[2],例如,当您调用chestLoot(2)时。

结构中的内存是按顺序排列的,因此chest.inventory[2] = HEALING_POTION_ID;将悄悄地覆盖chest.inventory[1]之后的4个字节,即chest.gold

好吧,"loot"结构的库存成员是一个长度为2的int数组。但您调用的"chestLoot"的值似乎大于1。这会导致你离开数组的末尾,写下"黄金"应该在哪里。