Pep3
Pep3

Reputation: 23

Simple c++ ATM (can't store balance variable input)

I'm making a simple c++ atm program, but I'm having trouble with getting the balance to change after I make a deposit or withdraw.

// C++ ATM

#include "std_lib_facilities.h"

int main()
{
    bool card_is_inserted = false;
    double balance = 0.0;
    //double new_balance = balance;


    // HOME

    //Starts over if variable is false
    while (card_is_inserted == false)
    {
        cout << "Wellcome to Well's Fargo ATM " << '\n'
        << "Insert card Yes or No"<< endl;
        string request;
        getline(cin,request);

        // Function is needed for aceppting different no's and yes's
        //-=-=-=--=-==--=-=-==-==-=--==--=-=-
        // loads atm
        if (request == "yes")
        {
            cout << "Alright, Your current balance is:" << endl
            << balance << endl;
            card_is_inserted = true;
        }

        // home
        string option = "cancel";

        while (card_is_inserted == true)
        {
            cout << "Would you like to withdraw or deposit? (Cancel)"<< endl;
            getline(cin,option);

            double cash_ = 0;

            if (option == "deposit")
            {
                cout << "How much money would you like to deposit?" << endl;
                cin >> cash_;
                double new_deposit_balance = balance + cash_;

                cout << "You placed: $" << cash_ << endl
                    << "Your New Balance is: $" << new_deposit_balance << endl;
            }

            if (option == "withdraw")
            {
                cout << "How much money would you like to withdraw?" << endl;
                cin >> cash_;
                double new_witdraw_balance = balance - cash_;

                if(balance <= 0)
                {
                    cout << "You don't have: $" << cash_ << endl;
                }
                else
                {
                cout << "You toke: $" << cash_ << endl
                << "Your New Balance is: $"<< new_witdraw_balance << endl;
                }
            }

            if (option == "cancel")
            {
                cout << "Ok, bye" << endl;
                card_is_inserted = false;
            }
        }
    }
}

example: I type yes to make a deposit(or withdraw) and then place a simple double like 12.50 then it shows me my current balance which will be 12.50; afterward I want to make a withdraw of 12.00 with .50 left. But I cant because the balance variable didn't store my previous value which was 12.50. I tried making "double new_balance = balance" but doesn't work like in swift.

Upvotes: 2

Views: 767

Answers (2)

frslm
frslm

Reputation: 2978

The line double new_deposit_balance = balance + cash_; only assigns the new balance to new_deposit_balance, but then you don't do anything with that variable (except print the value). If you want the new balance to persist, you actually need to modify balance, not new_deposit_balance, so something like balance = balance + cash_; or balance += cash_;.

The variable double new_deposit_balance only exists in the if-block it's defined in, so once you leave the if-block, you lose the information in new_deposit_balance. On the other hand, since balance is defined outside your if-blocks and while-loops, its value will persist throughout your ATM operations.

Of course, you'd also need to apply the same fix to new_witdraw_balance.

Upvotes: 0

Schmerzmittel
Schmerzmittel

Reputation: 40

You are not setting balance to new_witdraw_balance or new_deposit_balance.

double new_deposit_balance = balance + cash_; doesn't set the balance value because you are bring in the value of balance, but you are not assigning the outcome of balance + cash_ to balance.

You need to put something like balance = new_witdraw_balance; and balance = new_deposit_balance; at the end of each if after cout statement.

if(option == "deposit")
{
    cout << "How much money would you like to deposit?" << endl;
    cin >> cash_;
    double new_deposit_balance = balance + cash_;

    cout << "You placed: $" << cash_ << endl << "Your New Balance is: $" << new_deposit_balance << endl;

    balance = new_deposit_balance; // this
}

if(option == "withdraw")
{
    cout << "How much money would you like to withdraw?" << endl;
    cin >> cash_;
    double new_witdraw_balance = balance - cash_;

    if(balance <= 0)
    {
        cout << "You don't have: $" << cash_ << endl;
    }
    else
    {
        cout << "You toke: $" << cash_ << endl << "Your New Balance is: $"<< new_witdraw_balance << endl;
        balance = new_witdraw_balance; // and this
    }

}

Upvotes: 1

Related Questions