我正在编写一个模拟ATM的程序。因此,它可以非常基本地跟踪帐户余额,取款和存款。
在第一次迭代中,一切工作都很好,但是如果我要进行两次或两次以上的存款或取款,则帐户余额默认会恢复为原始金额。
以下是当前情况的一个示例:我的帐户最初有$ 1,000。我存了50美元。它显示我的帐户中现在有$ 1,050,并询问是否要执行其他任何操作。 (这一切都很好)。如果我选择再次存入$ 100,则表示我的新帐户余额为$ 1,100,而不是$ 1,150。当我执行新的提款或存款时,它不会存储我的最新帐户余额。
第二个(不太重要)的问题是,每次进行提款时,每次提款都会产生$ 2.50的费用,这笔费用也会从我的帐户余额中扣除。
我还没有学习过循环,只有Cases,If语句和If Else语句。
可以做我想做的事吗?下面是我的代码。先感谢您!这是我第一次发布,因此,如果我粘贴的代码错误,我深表歉意。
#include <iostream>
#include <string>
using namespace std; // opens library for "cout"
int main()
{
float test;
int logout;
string name;
float balance;
float fee;
int choice;
float withdraw;
float deposit;
float bonus;
bonus = 2.50;
balance = 1572.36;
fee = 12.50;
char answer;
cout << "Hello, thank you for banking with Pallet Town Bank.\n";
cout << "Please enter your name. ";
cin >> name;
cout << "Hello " << name << ". Your current balance is $" << balance << ".\n";
cout << "There will be a a service fee of $12.50 subtracted from your "
"account.\n";
cout << "Your updated balance will be $" << balance - fee << " \n";
cout << "What would you like to do today?\n";
do
{
cout << "\n1 - Current Balance\n2 - Withdraw\n3 - deposit\n4 - Log "
"Out\nOption: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\nCurrent Balance is " << balance - fee - withdraw + deposit
<< " \n";
cout << "Would you like to take any other actions today?\n";
break;
case 2:
cout << "\nWithdraw - How much would you like to withdraw? $";
cin >> withdraw;
cout << "Your new balance after withdrawing $" << withdraw << " will be $"
<< balance - fee - withdraw + deposit << "\n";
cout << "Would you like to take any other actions today?\n";
break;
case 3:
cout << "\nDeposit - How much would you like to deposit? $";
cin >> deposit;
test = balance - fee - withdraw + deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< test << endl; //<<balance - fee - withdraw + deposit<<"\n";
cout << "Would you like to take any other actions today? Y or N \n";
cin >> answer;
cout << answer;
if (answer == 'y' || 'Y')
{
test = balance - fee - withdraw + deposit + deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< test << endl;
}
// cout <<"Your new balance after depositing $"<<deposit<<" will be $"
// <<test<< endl; //<<balance - fee - withdraw + deposit<<"\n";
// cout <<"Would you like to take any other actions today?\n";
break;
case 4:
cout << "\nLog Out - Thank you for banking with Pallet Town Bank. Have "
"a great day!";
}
} while (choice != 4);
}
答案 0 :(得分:0)
正如艾伦·伯特尔斯(Alan Birtles)指出的那样,主要问题是您永远不会更新balance
值。您只需将临时计算存储在test
变量中即可。
这是更改代码的方式。我尝试从文本中尽力解释程序的预期行为:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const double fee = 12.50;
double balance = 1572.36;
cout << "Hello, thank you for banking with Pallet Town Bank.\n";
cout << "Please enter your name. ";
string name;
cin >> name;
cout << "Hello " << name << ". Your current balance is $" << balance << ".\n";
cout << "There will be a a service fee of $12.50 subtracted from your "
"account.\n";
cout << "Your updated balance will be $" << (balance -= fee) << " \n";
cout << "What would you like to do today?\n\n";
while (true)
{
cout << "1 - Current Balance" << '\n'
<< "2 - Withdraw" << '\n'
<< "3 - deposit" << '\n'
<< "4 - Log Out" << '\n'
<< "Option: ";
int choice;
cin >> choice;
cout << endl;
if (choice == 4) break;
switch (choice)
{
case 1:
cout << "Current Balance is " << balance << '\n';
break;
case 2:
cout << "Withdraw - How much would you like to withdraw? $";
double withdraw;
cin >> withdraw;
cout << "Your new balance after withdrawing $" << withdraw << " will be $"
<< (balance -= withdraw) << '\n';
break;
case 3:
cout << "Deposit - How much would you like to deposit? $";
double deposit;
cin >> deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< (balance += deposit) << '\n';
break;
}
cout << "Would you like to take any other actions today? ";
char answer;
cin >> answer;
cout << endl;
if (toupper(answer) == 'N') break;
}
cout << "Log Out - Thank you for banking with Pallet Town Bank. Have a great day!" << endl;
}
更改
首先,每次进行修改(取款/存款)时,我们都需要实际更新balance
。我们可以使用+=
或-=
运算符(称为“复合赋值”运算符)来实现:在编写balance += x
时,我们在{{1}上添加了x
},而在写balance
时我们就是在减去。这些表达式分别等效于balance -= x
和balance = balance + x
。
我将balance = balance - x
部分移到了"Would you like to [...]"
语句之外,以避免在每种情况下都重复。
如注释中所指出,switch
与answer == 'Y' || 'y'
不同。我将其更改为answer == 'Y' || answer == 'y'
。
我将注销处理移到了toupper(answer) == 'Y'
循环之外,以便无论循环何时终止,总是显示注销消息。这样一来,我们就可以从while
语句中删除case 4
,方法是先检查是否switch
,然后choice == 4
相应地退出循环。这也意味着该循环变为break
循环。也许有一种更优雅的方式。
如果您对函数感到满意,我建议您重构代码,分别隔离每个操作:
while (true)