0

I'm looking for a better way to define a global variable. This is just a small banking app to practice using functions. Is this the correct way to define the variable according to C++ standards? I'm not 100% sure on this one. I defined it outside of main(), but if I remember correctly, this is a no no. I tried creating classes, I tried creating paramaters for the functions, this is the only way I could figure out how to get the variable to all the functions.

// banking.cpp : This file contains the 'main' function. Program execution begins and ends there.


#include <iostream>

using namespace std;

int total = 100;


void menu();
int deposit();
int withdraw();

int main()
{
    menu();
    return 0;
}

void menu()
{

    int selection;
    cout << "your total money right now is $" << total << endl;
    cout << "select 1 for deposit." << endl;
    cout << "select 2 for withdraw" << endl;
    cin >> selection; 

    switch (selection)
    {
    case 1:
        deposit();
        break;
    case 2:
        withdraw();
        break;

    }

}

int deposit()
{
    int depositAmount;

    cout << "your current total is $" << total << endl;
    cout << "how much would you like to deposit? " << endl;
    cin >> depositAmount;

    total = depositAmount + total;

    cout << "your new total is $" << total << endl;
    cout << "you will be returned to the main menu..." << endl;

    menu();
    
    return total;
}

int withdraw()
{
    int withdrawAmount;
    
    cout << "your current total is $" << total << endl;
    cout << "how much would you like to withdraw? " << endl;
    cin >> withdrawAmount;

    total = total - withdrawAmount;

    cout << "your new total is $" << total << endl;
    cout << "you will be returned to the main menu..." << endl;

    menu();

    return total;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    The usage of non-const globals is discouraged but your syntax for declaring it is correct. – al3c Oct 28 '20 at 15:19
  • See [How to declare a global variable in C++](https://stackoverflow.com/questions/9702053/how-to-declare-a-global-variable-in-c) but in this specific case you could use a local variable and pass it around as function parameters. – Guy Incognito Oct 28 '20 at 15:20
  • 2
    *I'm looking for a better way to define a global variable* -- "Global variable" and "better" are two phrases that do not go together. – PaulMcKenzie Oct 28 '20 at 15:20
  • I think you've declared global variable. And in the same way the only correct way to declare global variable - not to declare and not to use it whenever you can. – Victor Gubin Oct 28 '20 at 15:20
  • the problem with globals is not that they are wrong, the problem is that they make it extremely difficult to convince yourself that the code is correct. Once you decided to use a global your code is ok. The best way to define a global is to not define it though – 463035818_is_not_an_ai Oct 28 '20 at 15:21
  • 7
    This looks like it would be better to have a class with `total` as a member variable and `deposit` and `withdraw` as member functions. – perivesta Oct 28 '20 at 15:22

2 Answers2

3

If you need to store a state shared across functions, you probably want to create a class/struct, with members as the data you want to share.

class MyClass
{
public:
   void menu();
   int deposit();
   int withdraw();

private:
   int total = 100;
};

Then use this class whenever you want to run your logic.

int main()
{
    MyClass myclass;
    myclass.menu();
    return 0;
}
Onur Onder
  • 306
  • 1
  • 4
  • One further step in refinement: turn `menu()` back into a free function. It's part of a banking application, but not part of a bank account. +1. – Pete Becker Oct 28 '20 at 16:49
2

You can pass total to a function with & in front of it. It is known as reference in C++ and you can modify given object inside a function, not only it's copy. Examples. And in your code:

int deposit(int &total) { ...your code... }

Overall I don't think calling menu inside every function is a good idea. With each function call you push it to a stack, which takes extra memory. Changing deposit and withdraw to void and using while(1) loop inside of main() to call menu() would be much better.

Dawid Wysocki
  • 61
  • 1
  • 3
  • 1
    Thank you sooooooooo much, i knew there was something going to bite me in the end by stacking the menu options. you answered my question perfectly! I do appreciate that, thank you. – Scotty Tollison Oct 28 '20 at 15:33