Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;不会打印某些变量_C++ - Fatal编程技术网

C++ C++;不会打印某些变量

C++ C++;不会打印某些变量,c++,C++,我无法将整个代码粘贴到这里,它对于主体来说太大了。这里有一个完整的粘贴(很抱歉,链接看起来有点不可信。) 然而,我相当肯定错误就在这些块的某个地方 这是所有项目、其变量和值的列表 void itemHeartCanister() { purchasableItem1Name = "Heart Canister"; itemHealthUpDescription = "Increases your maximum health by 1.";

我无法将整个代码粘贴到这里,它对于主体来说太大了。这里有一个完整的粘贴(很抱歉,链接看起来有点不可信。)

然而,我相当肯定错误就在这些块的某个地方

这是所有项目、其变量和值的列表

void itemHeartCanister()
    {
        purchasableItem1Name = "Heart Canister";
        itemHealthUpDescription = "Increases your maximum health by 1.";
        purchasableItem1Price = 25;
    }

void itemWoodenSword()
{
    purchasableItem2Name = "Wooden Sword";
    itemWoodenSwordDescription = "Increases your damage by 2.";
    purchasableItem2Price = 40;
}

void itemLeatherBoots()
{
    purchasableItem3Name = "Leather Boots";
    itemLeatherBootsDescription = "Increases your speed and defense by 1.";
    purchasableItem3Price = 30;
}

void itemGarnetNecklace()
{
    purchasableItem4Name = "Garnet Necklace";
    itemGarnetNecklaceDescription = "Increases your luck by 1.";
    purchasableItem4Price = 50;
}

void itemSpookyMemeStaff()
{
    purchasableItem5Name = "Spooky Meme Staff";
    itemSpookyMemeStaffDescription = "Fires spooky memes that do 4.20 damage each";
    purchasableItem5Price = 69;
}

void itemLeatherHelmet()
{
    purchasableItem6Name = "Leather Helmet";
    itemLeatherHelmetDescription = "A leather helmet that increases your defense by 2";
    purchasableItem6Price = 35;
}

void itemHealthPotion()
{
    purchasableItem7Name = "Health Potion";
    itemHealthPotionDescription = "A health potion that restores one health point";
    purchasableItem7Price = 20;
}

void itemManaPotion()
{
    purchasableItem8Name = "Mana Potion";
    itemManaPotionDescripton = "A potion that heals 10 mana points.";
    purchasableItem8Price = 20;
}

void itemEnergyPotion()
{
    purchasableItem9Name = "Energy Potion";
    itemEnergyPotionDescription = "A potion that heals 10 points of health and mana.";
    purchasableItem9Price = 45;
}

void itemStatusPotion()
{
    purchasableItem10Name = "Status Potion";
    itemStatusPotionDescription = "A potion that removes all bad status effects.";
    purchasableItem10Price = 120;
}

void itemIceSword()
{
    purchasableItem11Name = "Ice Sword";
    itemIceSwordDescription = "A sword made of ice. Has a 40% chance to freeze enemies for 2 seconds.";
    purchasableItem11Price = 150;
}

void itemBomb()
{
    purchasableItem12Name = "Bomb";
    itemBombDescription = "A bomb. Blows stuff up.";
    purchasableItem12Price = 30;

}

void itemToyPlane()
{
    purchasableItem13Name = "Toy Plane";
    itemToyPlaneDescription = "A toy plane. Does absolutely nothing.";
    purchasableItem13Price = 50;
}
void shopItemDatabase()
{
    itemHeartCanister();
    itemWoodenSword();
    itemLeatherBoots();
    itemGarnetNecklace();
    itemSpookyMemeStaff();
    itemLeatherHelmet();
    itemEnergyPotion();
    itemStatusPotion(); 
    itemIceSword();
    itemBomb();
    itemToyPlane();
}
以下是“购买”菜单的其余代码:

void item1()
{
    if(!isItem1Randomized)
    {
        shopItem1 = rand()%4+1;
        isItem1Randomized = true;
    }
    if(shopItem1 == 1)
    {
        shopItem1Name = purchasableItem1Name;
        shopItem1Description = itemHealthUpDescription;
        shopItem1Price = purchasableItem1Price;
    }
    else if(shopItem1 == 2)
    {
        shopItem1Name = purchasableItem2Name;
        shopItem1Description = itemWoodenSwordDescription;
        shopItem1Price = purchasableItem2Price;
    }
    else if(shopItem1 == 3)
    {
        shopItem1Name = purchasableItem3Name;
        shopItem1Description = itemLeatherBootsDescription;
        shopItem1Price = purchasableItem3Price;
    }
    else if(shopItem1 == 4)
    {
        shopItem1Name = purchasableItem4Name;
        shopItem1Description = itemGarnetNecklaceDescription;
        shopItem1Price = purchasableItem4Price;
    }
}
void item2()
{
    if(!isItem2Randomized)
    {
        shopItem2 = rand()%4+1;
        isItem2Randomized = true;
        cout << shopItem2;
    }
    if(shopItem2 == 1)
    {
        shopItem2Name = purchasableItem5Name;
        shopItem2Description = itemSpookyMemeStaffDescription;
        shopItem2Price = purchasableItem5Price;
    }
    else if(shopItem2 == 2)
    {
        shopItem2Name = purchasableItem6Name;
        shopItem2Description = itemLeatherHelmetDescription;
        shopItem2Price = purchasableItem6Price;
    }
    else if(shopItem2 == 3)
    {
        shopItem2Name = purchasableItem7Name;
        shopItem2Description = itemHealthPotionDescription;
        shopItem2Price = purchasableItem7Price;
    }
    else if(shopItem2 == 4)
    {
        shopItem2Name = purchasableItem8Name;
        shopItem2Description = itemManaPotionDescripton;
        shopItem2Price = purchasableItem8Price;
    }
}
void item3()
{
    if(!isItem3Randomized)
    {
        shopItem3 = rand()%4+1;
        isItem3Randomized = true;
    }
    if(shopItem3 == 1)
    {
        shopItem3Name = purchasableItem9Name;
        shopItem3Description = itemHealthUpDescription;
        shopItem3Price = purchasableItem9Price;
    }
    else if(shopItem3 == 2)
    {
        shopItem3Name = purchasableItem10Name;
        shopItem3Description = itemWoodenSwordDescription;
        shopItem3Price = purchasableItem10Price;
    }
    else if(shopItem3 == 3)
    {
        shopItem3Name = purchasableItem11Name;
        shopItem3Description = itemLeatherBootsDescription;
        shopItem3Price = purchasableItem11Price;
    }
    else if(shopItem3 == 4)
    {
        shopItem3Name = purchasableItem12Name;
        shopItem3Description = itemGarnetNecklaceDescription;
        shopItem3Price = purchasableItem12Price;
    }
}
void item4()
{
    if(!isItem4Randomized)
    {
        shopItem4 = rand()%4+1;
        isItem4Randomized = true;
    }
    if(shopItem4 == 1)
    {
        shopItem4Name = purchasableItem13Name;
        shopItem4Description = itemHealthUpDescription;
        shopItem4Price = purchasableItem13Price;

    }
    else if(shopItem4 == 2)
    {
        shopItem4Name = purchasableItem14Name;
        shopItem4Description = itemWoodenSwordDescription;
        shopItem4Price = purchasableItem14Price;
    }
    else if(shopItem4 == 3)
    {
        shopItem4Name = purchasableItem15Name;
        shopItem4Description = itemLeatherBootsDescription;
        shopItem4Price = purchasableItem15Price;
    }
    else if(shopItem4 == 4)
    {
        shopItem4Name = purchasableItem16Name;
        shopItem4Description = itemGarnetNecklaceDescription;
        shopItem4Price = purchasableItem16Price;
    }
}

void itemSelector()
{
    item1();
    item2();
    item3();
    item4();
}
void buyMenu()
{
    if(mazeActive == 2)
    {
        if((shop1[17][17] == shop1[playerY][playerX]))
        {
            isEnteredShop = true;
        }
        else
        {
            isEnteredShop = false;
        }
    }
    if(isEnteredShop)
    {
        itemSelector();
        cout << "You have entered the shop. Items to buy: \n";

        if(!isItem1Bought)
        {
            cout << "1. " << shopItem1Name << ":\n"
                 << shopItem1Description << "\n" << "$" << shopItem1Price << "\n \n";
        }
        if(!isItem2Bought)
        {
            cout << "2. " << shopItem3Name << ":\n"
                 << shopItem3Description << "\n" << "$" << shopItem3Price << "\n \n";
        }
        else
        {
            cout << "Something broke";
        }
        if(!isItem3Bought)
        {
            cout << "3. " << shopItem2Name << ":\n"
                 << shopItem2Description << "\n" << "$" << shopItem2Price << "\n \n";
        }
        if(!isItem4Bought)
        {
            cout << "4. " << shopItem4Name << ":\n"
                 << shopItem4Description << "\n" << "$" << shopItem4Price << "\n \n";
        }

        cout << "5. Exit shop. \n";

        cin >> shop1ItemBought;
        if((shop1ItemBought == 1) && (shopItem1Name == purchasableItem1Name))
        {
            playerHealthPoints = playerHealthPoints + itemHeartCanisterPlayerStatisticChanges;
            playerCurrency = playerCurrency - purchasableItem1Price;
            isItem1Bought = true;
            isMoved = true;
        }
        else if((shop1ItemBought == 1) && (shopItem1Name == purchasableItem2Name))
        {
            playerDamage++;
            playerCurrency = playerCurrency - purchasableItem2Price;
            isItem1Bought = true;
            isMoved = true;
        }
        else if((shop1ItemBought == 1) && (shopItem1Name == purchasableItem3Name))
        {
            speedStatistic++;
            playerCurrency = playerCurrency - purchasableItem3Price;
            isItem1Bought = true;
            isMoved = true;
        }
        else if((shop1ItemBought == 1) && (shopItem1Name == purchasableItem4Name))
        {
            playerDamage++;
            playerCurrency = playerCurrency - purchasableItem4Price;
            isItem1Bought = true;
            isMoved = true;
        }
        else if((shop1ItemBought == 2) && (shopItem1Name == purchasableItem5Name))
        {
            playerCurrency =- purchasableItem5Price;
            hasSpookyMemeStaff = true;
            isItem2Bought = true;
        }
        else if((shop1ItemBought == 2) && (shopItem1Name == purchasableItem6Name))
        {
            playerCurrency = playerCurrency - shopItem2Price;
            playerDefense+=2;
            isItem2Bought = true;
        }
        else if((shop1ItemBought == 2) && (shopItem1Name == purchasableItem7Name))
        {
            playerCurrency = playerCurrency - shopItem2Price;
            playerHealthPoints++;
            isItem2Bought = true;
        }
        else if((shop1ItemBought == 2) && (shopItem1Name == purchasableItem8Name))
        {
            playerCurrency = playerCurrency - shopItem2Price;
            playerMana+=10;
            isItem2Bought = true;
        }
        isEnteredShop = false;
    }
}
void item1()
{
如果(!IsItem1随机化)
{
shopItem1=rand()%4+1;
IsItem1随机化=真;
}
如果(shopItem1==1)
{
shopItem1Name=可购买项目1Name;
shopItem1Description=itemHealthUpDescription;
ShopItem1价格=可购买的Item1价格;
}
else if(shopItem1==2)
{
ShopItem1名称=PurchasableItem2名称;
shopItem1Description=itemWoodenSwordDescription;
ShopItem1价格=可购买的Item2价格;
}
else if(shopItem1==3)
{
shopItem1Name=purchasableItem3Name;
shopItem1Description=itemLeatherBootsDescription;
ShopItem1价格=可购买的Item3价格;
}
else if(shopItem1==4)
{
shopItem1Name=purchasableItem4Name;
shopItem1Description=itemGarnetNecklaceDescription;
ShopItem1价格=可购买项目4价格;
}
}
作废项目2()
{
如果(!IsItem2随机化)
{
shopItem2=rand()%4+1;
isItem2Randomized=真;

看了一眼,你好像把你的2和3弄混了

    if(!isItem2Bought)
    {
        cout << "2. " << shopItem3Name << ":\n"
             << shopItem3Description << "\n" << "$" << shopItem3Price << "\n \n";
    }
    else
    {
        cout << "Something broke";
    }
    if(!isItem3Bought)
    {
        cout << "3. " << shopItem2Name << ":\n"
             << shopItem2Description << "\n" << "$" << shopItem2Price << "\n \n";
    }
如果(!IsItem2Buw)
{

cout您的编程风格涉及大量代码重复,这会导致错误。@AJNeufeld已经向您展示了如何混淆某些变量名。如果您的变量名中有数字,并且这些数字可能大于2,则很可能您应该使用数组。 另一个问题是,您有
shopItemXName
shopItemXDescription
等等,或者将许多商店商品的属性放在其他地方。最好将这些东西分组到一个结构中。然后您可以执行以下操作:

struct ShopItem {
    char *name;
    char *description;
    int price;
    bool bought;
};

const int nItems = 4;
struct ShopItem shopItems[nItems];
std::vector<item> database { 
    { "Heart Canister", "Increases your maximum health by 1.", 25 },
    { "Wooden Sword", "Increases your damage by 2.", 40 },
    /* ... */
    { "Toy Plane", "A toy plane. Does absolutely nothing.", 50 }
};
for (int i : chosen_items)
    display(database[i]);
稍后,您可以将大量重复代码重构为for循环,如下所示:

for (int i = 0; i < nItems; i++) {
    if (!shopItems[i].bought) {
        cout << i << ". " << shopItems[i].name << ":\n"
             << shopItems[i].description << "\n"
             << "$" << shopItems[i].price << "\n \n";
    }
}
for(int i=0;icout我的建议是从键盘后退一步。深呼吸。从1000到200倒数7秒

现在仔细想想你在这里做什么。这里有相当一部分代码实际上只是数据。如果你将数据表示为实际数据,那会更好(至少在我看来)

让我们从项目列表开始。每个项目似乎有三个元素:名称、说明和价格。因此,让我们定义一个结构:

struct item { 
    std::string name;
    std::string description;
    int price;
};
有了它,我们可以定义项目表,如下所示:

struct ShopItem {
    char *name;
    char *description;
    int price;
    bool bought;
};

const int nItems = 4;
struct ShopItem shopItems[nItems];
std::vector<item> database { 
    { "Heart Canister", "Increases your maximum health by 1.", 25 },
    { "Wooden Sword", "Increases your damage by 2.", 40 },
    /* ... */
    { "Toy Plane", "A toy plane. Does absolutely nothing.", 50 }
};
for (int i : chosen_items)
    display(database[i]);
由于我们的项目在一个向量中,我们可以按数字选择四个项目:

std::vector<size_t> chosen_items;

for (int i=0; i<16; i+= 4)
    chosen_items.push_back(rand_range(i, i+4));

这仍有待进一步改进,但我认为一般的方法可能会产生比代码前进方向更令人满意的结果。

听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看它在哪里运行如果你要做任何编程,这是一个必不可少的工具。进一步的阅读:如果你张贴一个(最小的强调),可能有人会去调试你的代码。看起来像一个复制粘贴错误。考虑使用向量(甚至数组)。项和循环。您可以通过使用结构和容器简化代码。通过使用结构的
std::vector
可以消除许多函数。