Recall a function multiple times, but with variable values stored from last time

Viewed 228

I've tried to make a small game where you choose what are you planting, with a money system.

I created the function soldSystem in which I use switch for type of plants and how much each costs. And in main I print the info about the plants and with an if I've printed the money. It doesn't work as I want. An example: I have 100$ and with 50 i planted somthing, and when I sold it I have 150$, but when I plant another thing, it starts from 100$ again.


int soldSystem(int moneyNum) {
    int money;
    money = 100;
    int i;
    i = 1;
    while (i < 2) {
        switch (moneyNum)
        {
        case 0: {
            if (money >= 50) {
                cout << "Tomatoes planted\n";
                money = money - 50;
                cout << "You have :" << money << " Dollars" << endl;
                cout << "\n";
                cout << "Tomatoes Sold\n";
                money = money + 100;
            }
            else cout << "Don't have enough money\n";
        }


                break;
        case 1: {
            if (money >= 100) {
                cout << "Carrots planted";
                money = money - 100;
                cout << "You have:" << money << " Dollars" << endl;
                cout << "\n";
                cout << "Carrots Sold\n";
                money = money + 300;
            }
            else cout << "Don't have enough money\n";
        }


                break;
        case 2: {
            if (money >= 300) {
                cout << "Mushrooms planted\n";
                money = money - 500;
                cout << "You have:" << money << " Dollars" << endl;
                cout << "\n";
                cout << "Mushrooms Sold\n";
                money = money + 1000;
            }
            else cout << "Don't have enough money\n";
        }


                break;


        default:
            cout << "Try again\n";
            money = money;
            break;
        }
        i++;
        

    }


    return money;
}


int main()
{
    int i,playerChoose;
    i = 1;
    while (i > 0) {
        cout << "Choose what to plant\n";
        cout << "0-Tomatoes(50/100);1-Carrots(100/300);2-Mushrooms(500/1000)\n";
        cin >> playerChoose;
        if (-1 < playerChoose < 3) {
            cout << "You have:" << soldSystem(playerChoose) << endl;
            cout << "\n";
            
        }
    }
    cout <<"You have:"<<soldSystem << endl;
    
    return 0;
}
5 Answers

You need to use static storage class if you need to retain the values between function calls. Static variables will be initialized only once. You can declare 'money' variable like below inside the function.

static int money;

But the problem with your code is you are assigning value in the next statement after the declaration. So it becomes

static int money;
money = 1500;

So during each time of 'soldSystem' function call, money will be assigned the value 1500.

To fix this you can combine both statements into one as below.

static int money = 1500;

Now as static variables will be initialized only once, when you enter the function first time, money is assigned 1500.

Make money a static variable. This will cause it to retain its value from one call to the next:

static int money = 100;

I'd declare money before declaring soldSystem() and then initialize it in main().

As an alternative to using static or a global variable, consider an Object Oriented approach, where SellSystem is a class, with sell() as a function and money stored inside the class.

This would be declared like this:

/// sold system class
class SoldSystem {
public:
    /// constructor, set's the money to 100
    SoldSystem() :
        money(100)
    {}
    /// your soldSystem() function, but without money declared in it (it's declared with the class)
    int sell(int moneyNum) {
        int i;
        ...
        return money;
    }
private:
    /// the money in the sold system
    int money;
};

The object would be created like:

/// create the SoldSystem object
SoldSystem soldSystem;

and would be used like:

/// call sell() function on soldSystem
cout << "You have:" << soldSystem.sell(playerChoose) << endl;

This has the advantage of being used in multiple contexts. (For example, you could have two SoldSystem objects for two different people, each with their own money).

Let try a lambda.

(totally untested code)

auto soldSystem = [money = 100](int moneyNum)  mutable {
    using std::cout;
    using std::endl;
    int i = 1;
    while (i < 2) {
        switch (moneyNum) {
        case 0: {
            if (money >= 50) {
                cout << "Tomatoes planted\n";
                money = money - 50;
                cout << "You have :" << money << " Dollars" << endl;
                cout << "\n";
                cout << "Tomatoes Sold\n";
                money = money + 100;
            } else
              cout << "Don't have enough money\n";
        }
          break;
        case 1: {
            if (money >= 100) {
                cout << "Carrots planted";
                money = money - 100;
                cout << "You have:" << money << " Dollars" << endl;
                cout << "\n";
                cout << "Carrots Sold\n";
                money = money + 300;
            }
            else cout << "Don't have enough money\n";
        }
        break;
        case 2: {
            if (money >= 300) {
                cout << "Mushrooms planted\n";
                money = money - 500;
                cout << "You have:" << money << " Dollars" << endl;
                cout << "\n";
                cout << "Mushrooms Sold\n";
                money = money + 1000;
            }
            else cout << "Don't have enough money\n";
        }
        break;

        default:
            cout << "Try again\n";
            //money = money; // doesnt do anything
            break;
        }
        i++;
    }

    return money;
};

int main() {
  int i,playerChoose;
  i = 1;
  while (i > 0) {
      std::cout << "Choose what to plant\n";
      std::cout << "0-Tomatoes(50/100);1-Carrots(100/300);2-Mushrooms(500/1000)\n";
      std::cin >> playerChoose;
      if (-1 < playerChoose && playerChoose < 3) {
          std::cout << "You have:" << soldSystem(playerChoose) << std::endl;
          std::cout << "\n";

      }
  }
  std::cout <<"You have:"<< soldSystem(-1) << std::endl;

  return 0;
}

Maybe this was a xy-problem instead.

Related