0

I've been teaching myself C++ for the past few hours (I have previous coding experience, but only very basic). I'm wondering, how would I allow the user to change the value of a variable between different functions?

Edit: I do have an intermediate knowledge of Python from school, but it has completely left my mind. I'm basically asking for refreshers, not tutorials. Once I'm reminded, I'll be all set. Sorry for the confusion.

int shop() {
    int selection;
    cout << "This is the shop!" << endl;
    cout << "What would you like to browse?: " << endl;
    cin >> selection;
    switch (selection) {
        case1: {
            int selection;
            cout << "Farmer Market!\n1) Seeds (T1)\n2) Seeds (T2)\n3) Seeds (T3)\n4) Tools (T1)\n5) Tools (T2)\n6) Tools (T3)" << endl;
            cout << "What would you like to buy?: ";
            switch (selection) {
                case 1: {
                    items = 1; ///THIS IS THE PART I NEED HELP WITH
                }
            }
        }
    }
}

int inventory() {
    int items;
    int tools;
    return items, tools;

How would I allow the user to buy some seeds in the function shop(), then assign a value of 1 to the items variable in the function inventory(), without needing to assign a global variable? I need items to retain its value throughout the rest of the game.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Liamvvvv
  • 3
  • 3
  • Does your C++ book cover function arguments? – Stephen Newell Dec 10 '20 at 20:37
  • You might want to read [a good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Quimby Dec 10 '20 at 20:37
  • At a few hours in with only basic coding experience, you probably shouldn't even be messing with functions other than `main`. Take it slow, learn some control flow, then you can tackle procedural programming. – Silvio Mayolo Dec 10 '20 at 20:39
  • What book would you reccomend, i dont have one but im after something that covers all the basics --> intermediate/advanced without being too dumbed down as i can pick things up very quickly – Liamvvvv Dec 10 '20 at 20:43
  • 1
    See the link in Quimby's comment for a curated list of books that are widely accepted as good. – user4581301 Dec 10 '20 at 20:48
  • @Liamvvvv *A tour of C++* might be suitable for you then, you can always search the internet for specific stuff. – Quimby Dec 10 '20 at 20:49

2 Answers2

1

You could pass the variable item by reference.

int shop(int& itemsRef) {
   //Do calculations like itemsRef = 1
}

int inventory() {
   int items;
   shop(items);
}

Passing something by reference just means, that the program will not copy the value of items into a new variable that the function then uses. ItemsRef wird ein Verweis auf die Variable items in inventory. itemsRef becomes a reference to the variable items in inventory. The variable itmesRef is then treated exactly like items. The advantage of this is that you don't have to return a return value, so you don't have to return itemsRef. Here is some more information: learncpp This site is also a great resource for learning c++.

  • @Liamvvvv Happy that i could help, but if you really use the variable `items` and `tools` over and over again, a `class` might be better. If you want to make a game in your example by having an inventory and a shop, classes would be smarter. – Hydrated Dragon Dec 10 '20 at 21:28
1

WARNING: this most likely is above your level. Change and use code with caution.

You could use a class for this. For example,

class ShopInventory{
  public:
    int items;
    int tools;
    ShopInventory(){
      items=0;
      tools=0;
    }
    ~ShopInventory(){}
}inventory;

int shop(){
  int selection;
  cout << "What would you like to buy?\n\t(1) seeds\n\t(2) hammer";
  cin >> selection;
  switch(selection){
    case 1:
      inventory.items++;
      cout << "You have bought seeds";
    case 2:
      inventory.tools++;
      cout << "You have bought a hammer";
  }
}

The class stores your amount of items and tools. You can create one by calling the constructor ShopInventory(), or by adding it to the end of the class definition (}inventory,inventory2;). If you want it to be not in global scope, you can move the class definition into the shop() function.

You can get the amount of items by inventory.items and tools by inventory.tools. You can edit them as if they were normal variables too:

inventory.items += 10; // bought 10 items
inventory.tools += 3; // bought 3 tools

You can also extend this by adding more into the class.

Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34