'Retain value from a certain function

I am new to C++ and here I am trying to create a simple ATM algorithm. basically the problem I have is to update the value of "balance" after adding value to my deposit variable.

Initially I want to deposit here an amount of 100, and on my output my balance is successfully updated, but when I went back to my main menu, my balance was reset to my default value.

Here's my sample output

Here's my script:

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <chrono>
#include <thread>
using namespace std;

bool Pin(int N);
double Balance(double balance,double deposit, double withdraw);
double Deposit(int choice, double deposit);
double Withdraw(double withdraw);
void MainMenu();
void Option(int choice);

int main()
{
int choice,pin;
//check balance, deposit money, withdraw, show menu
cout << "Please enter 4 digit pin number: ";
cin >> pin;
if (Pin(pin)==1)
{
    MainMenu();
    cin >> choice;
    Option(choice);
}
system("pause >0");

}

void delay(int x)
{
std::this_thread::sleep_for(std::chrono::milliseconds(x));
}

double Balance(double balance = 500, double deposit = 0, double withdraw = 0) //function to return balance value {
balance += deposit;
balance -= withdraw;
return balance;
}

double Deposit(int choice=0,double deposit=0) //function to perform deposit
{
double balance = Balance();
if (choice != 0)
{
    cout << "Please confirm deposit amount: " << deposit << endl;
    cout << "1. Yes" << endl;
    cout << "2. No" << endl;
    cout << "Please enter option: ";
    cin >> choice;
    if (choice == 2)
        while (2)
        {
            cout << "Enter amount to deposit: ";
            cin >> deposit;
            cout << "\nPlease confirm deposit amount: " << deposit << endl;
            cout << "1. Yes" << endl;
            cout << "2. No" << endl;
            cout << "Please enter option: ";
            cin >> choice;
        }
}
cout << "Your new balance is: " << Balance(balance, deposit) << endl;
return deposit; 
 }

double Withdraw(double withdraw=0) //function to perform withdrawal
{
double balance = Balance();
double deposit = Deposit();
int choice;
cout << "Please take card" << endl << endl;
delay(1500);
cout << "Would you like to check balance?" << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
cout << "Please enter option: ";
cin >> choice;
if (choice == 1)
    cout << "Your new balance is: " << Balance(balance, deposit, withdraw) <<endl;
return withdraw;
    }

void MainMenu() //Function for main menu
{
Balance();
cout << "* * * * * * ATM MACHINE MENU * * * * * *  " << endl;
cout << "1. Check Balance" << endl;
cout << "2. Deposit" << endl;
cout << "3. Withdraw" << endl;
cout << "4. Exit" << endl;
cout << "* * * * * * * * * * * * * * * * * * * * " << endl << endl;
cout << "Please enter option: ";
}

void Option(int choice)
{
int pin;
double deposit, withdraw;
switch (choice)
{
case 1: cout << "Your Balance is " << Balance(); break;

case 2: cout << "Enter amount to deposit: ";
    cin >> deposit; Deposit(1,deposit);
    cout << "Do you have other transaction? " <<endl;
    cout << "1. Yes" << endl;
    cout << "2. No" << endl;
    cout << "Please enter option: ";
    cin >> choice;
    if (choice == 1)
        main();
    break;
    
case 3: cout << "Enter withdrawal amount: ";
    cin >> withdraw; 
    cout << "Please enter 4 digit pin number: ";
    cin >> pin;
    if (Pin(pin) == 1)
    Withdraw(withdraw); break;
default:
    break;
}

}
c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source