想知道如何将一大块代码放入一个单独的函数中并在它们之间共享变量

Wondering how I can put a chunk of code into a separate function and share variables between them

我目前正在尝试构建一个简单的店主程序,您可以从中购买商品。这些项目存储在一个数组中,您放入数组的项目数量有上限。

虽然我的程序可以运行,但我不确定如何在两个函数之间共享变量

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

int PurchaseItems()
{
    //I'd like to switch statement to go in here
}

int main()
{
    const int maxItems = 5; //Maximum number of items in inventory
    int userChoice = 0, //User control for menus
        goldPieces = 75, //How many gold pieces player has
        numbItems = 0; //Current number of items in inventory

std::string inventory[maxItems]; //Inventory

//Items in inventory when player begins
inventory[numbItems++] = "Sword";
inventory[numbItems++] = "Cloak";
inventory[numbItems++] = "Boots";

std::cout << "Greeting traveller! Feel free to browse my wares."; //Greeting message

do
{
    //List of things to buy
    std::cout << "\n1: Axe - 25gp.\n2: Mace - 30gp. \n3: Helmet - 20gp. \n4: Exit.\n";
    std::cin >> userChoice; //Reads user input

    switch (userChoice)
    {
    case 1:
        if (goldPieces >= 25 && numbItems < maxItems) //If userhas enough gold and available inventory space
        {
            inventory[numbItems++] = "Axe"; //Add to inventory
            goldPieces -= 25; //Remove gold from player
        }

        else
        {
            std::cout << "You do not meet the requirements.";
        }
    break;

    case 2: 
        if (goldPieces >= 30 && numbItems < maxItems)
        {
            inventory[numbItems++] = "Mace";
            goldPieces -= 30;
        }

        else
        {
            std::cout << "You do not meet the requirements.";
        }
    break;

    case 3: 
        if (goldPieces >= 20 && numbItems < maxItems)
        {
            inventory[numbItems++] = "Helmet";
            goldPieces -= 20;
        }

        else
        {
            std::cout << "You do not meet the requirements.";
        }
    break;

    case 4:
        std::cout << "You have left the shop.\n";
    break;

    default:
        std::cout << "Please enter sufficient data.";
        break;
    }

    //List inventory
    std::cout << "Inventory: \n";

    for (int i = 0; i < numbItems; i++)
    {
        std::cout << inventory[i] << "\n";
    }
} while (userChoice != 4);




//keep window open
std::string barn;
std::cin >> barn;

return 0;

}

虽然我认为我可以拥有它以便在另一个函数中处理黄金,但我不确定如何获得 'numbItems' 和 'maxItems' 变量。

提前谢谢你。

您是否有不想使用参数的原因?此外,由于这是 C++,您应该使用向量而不是数组。

int PurchaseItems(std::vector<std::string>& items, /*more parameters*/)
{
    //Use items here
    items.push_back("Axe"); //Buy an axe
}

//elsewhere
std::vector<std::string> items;
int result = PurchaseItems(items, /*more arguments*/);

您基本上有两种选择 - 将参数传递给函数(和 return 值)或使用全局变量。

全局变量非常不受欢迎(这是理所当然的!),因为它们使程序难以维护、理解、测试和调试。因此,您需要传递参数。这是您可以执行此操作的一种(不是唯一一种!)方法:

// I did remove max_items check for now
int PurchaseItems(std::vector<std::string>& inventory, int gold, int choice)
{
  switch (choice)
    {
    case 1:
        if (gold >= 25) //If userhas enough gold and available inventory space
        {
            inventory.push_back("Axe"); //Add to inventory
            gold -= 25; //Remove gold from player
        }
...
}
...
   // then later in your program:
   goldPieces = PurchaseItems(inventory, goldPieces);