简单的c ++ ATM(不能存储平衡变量输入)

时间:2017-10-01 17:16:14

标签: c++

我正在制作一个简单的c ++ atm程序,但是在我存款或取款后我无法获得余额变更。

// 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;
            }
        }
    }
}

例子:我输入是存款(或取款),然后放一个像12.50这样的简单双,然后它显示我当前的余额为12.50;之后我想以0.50左右退出12.00。但我不能因为余额变量没有存储我之前的12.50值。我尝试制作&#34; double new_balance = balance&#34;但是并不像swift那样工作。

2 个答案:

答案 0 :(得分:1)

您没有将balance设置为new_witdraw_balancenew_deposit_balance

double new_deposit_balance = balance + cash_;未设置balance值,因为您引入了balance的值,但您没有将balance + cash_的结果分配给{{1} }}

您需要在balance语句之后的每个balance = new_witdraw_balance;末尾添加balance = new_deposit_balance;if之类的内容。

cout

}

答案 1 :(得分:0)

double new_deposit_balance = balance + cash_;行只会将新余额分配给new_deposit_balance,但您不会对该变量执行任何操作(除了打印该值)。如果您希望新余额保持不变,则实际上需要修改balance,而不是new_deposit_balance,因此需要balance = balance + cash_;balance += cash_;

变量double new_deposit_balance仅存在于其定义的if-block中,因此一旦离开if-block,就会丢失new_deposit_balance中的信息。另一方面,由于balance是在if-blocks和while-loops之外定义的,因此它的值将在整个ATM操作期间保持不变。

当然,您还需要对new_witdraw_balance应用相同的修补程序。