gpt4 book ai didi

c++ - 简单的 c++ ATM(不能存储余额变量输入)

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:08:05 25 4
gpt4 key购买 nike

我正在制作一个简单的 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;
}
}
}
}

示例:我输入 yes 进行存款(或取款),然后输入一个简单的 double,例如 12.50,然后它会显示我的当前余额为 12.50;之后,我想在 12.00 时提款,还剩 0.50。但我不能,因为余额变量没有存储我之前的值 12.50。我尝试制作“double new_balance = balance”,但无法像在 swift 中那样工作。

最佳答案

您没有将 balance 设置为 new_widraw_balancenew_deposit_balance

double new_deposit_balance = balance + cash_; 没有设置 balance 值,因为你引入了 balance 的值,但是你没有将 balance + cash_ 的结果分配给 balance

你需要在 cout 之后的每个 if 的末尾添加类似 balance = new_widraw_balance;balance = new_deposit_balance; 的内容 语句。

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
}

关于c++ - 简单的 c++ ATM(不能存储余额变量输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com