有没有一种方法可以使用变量来引用c ++中的其他变量?

时间:2020-08-21 12:01:16

标签: c++ variables

我正在尝试使用C ++编写基于文本的冒险游戏,游戏的一部分包括一家商店,其中出现了一些随机物品。我是C语言的新手,所以如果我的技巧是业余的,请原谅,但是我正在做的是运行一个rand变量,并使用if/else if链来确定每个项目的内容。

if (restock == 0)
    {
        srand((unsigned int)time(NULL));
        store_item_1_variable = rand() % 99;
        store_item_1_variable = store_item_1_variable + 1;
        if (10 >= store_item_1_variable && store_item_1_variable > 0)
        {
            item_1_name = "The Staff of Beetles";
            item_1_price = 120;
            /**store_item_1 = int StaffOfBeetles;**/

        }
        else if (30 >= store_item_1_variable && store_item_1_variable > 0)
        {
            item_1_name = "Health Potion";
            item_1_price = 15;
            /**store_item_1 = int HealthPotions;**/  
            // I want to increment store_item_1 later
        }
        // else if (etc)
      }

稍后在代码中,当玩家选择“是”购买对象时,我想要以下内容:

std::cout << item_1_name;
  while (valid_input == false)
  {
   std::cin >> text_input;
   const char* text_output = text_input.c_str();
   if(strcmp(text_output, "Yes") == 0 ||  strcmp(text_output, "1") == 0)
   {
    gold = gold - item_1_price 
    store_item_1 = store_item_1 + 1; //When you purchase the object, you get +1 of it in you inventory
    valid_input = true;
    }
    // ... yadda yadda yadda

第二个代码中我想要的是将1添加到我之前定义的带星号的任何int定义中,但是我不知道该怎么做。有帮助吗?

2 个答案:

答案 0 :(得分:0)

您可以制作classstruct

#include <string>

struct Item
{
    Item(std::string _name, int _price) : name(_name), price(_price) {}

    std::string name;
    int price;
};

然后像使用它

Item store_item_1;

if (10 >= store_item_1_variable && store_item_1_variable > 0)
{
    store_item_1 = Item("The Staff of Beetles", 120);
}
else if (30 >= store_item_1_variable && store_item_1_variable > 0)
{
    store_item_1 = Item("Health Potion", 15);
}

然后,如果您想了解有关商品的信息

std::string name = store_item_1.name;
int price = store_item_1.price;

如果要制作其中几个,则可以将它们添加到std::vector<Item>并像引用它们一样

std::vector<Item> items;
// fill this out
std::string third_name = items[2].name;

或者可以遍历它们

for (Item const& item : items)
{
    std::cout << item.name << ' ' << item.price << '\n';
}

答案 1 :(得分:0)

您正在寻找指针。这是一个非常快速的介绍:

int *selected_item;
// selected_item is a variable which references an int variable.
// (Don't know which one yet!)

// Note: If you do *selected_item here, your program will probably crash,
// but it might also cause different problems, depending on your luck.
// So don't do *selected_item before selected_item refers to an actual variable.

selected_item = &a;
// selected_item references variable a

*selected_item = 5;
// Set the variable which selected_item references to 5.
// a is now 5.

if (something) {
    selected_item = &b;
} else {
    selected_item = &c;
}

// selected_item references variable b or c

*selected_item = *selected_item * 3;
// Set the variable which selected_item references, to whatever was in it before, times 3.
// Either b or c is now 3 times what it was previously.

如果您因为觉得学习无聊而推迟学习指针,那么我希望这会让您想要学习它们。

请注意,除非*selected_item引用仍然存在的有效变量,否则不得使用selected_item!否则,您将获得“不确定的行为”,这意味着您的程序可能会崩溃,或者它可能会做一些奇怪的事情,并且使您难以尝试找出问题所在。