我不知道怎么写这段代码
Cannot figure out how to do this code
我一直在做这个模拟银行账户的项目。用户可以存款、取款,并在屏幕上显示所有的取款和存款。在选择菜单的顶部需要是当前的余额。像这样,储蓄:100。无论何时我存钱或取款,我都需要把余额变成正确的金额。起价为100美元。如果我存或取钱,第一次它会正常工作,但第二次它会被重置为100美元,然后交易完成。我怎样才能使余额保持在正确的金额而不重置?这是一个学校的项目,我不是找人给我提供代码。我只是寻求一些提示或指导在正确的方向。下面是我的int main()的代码:
int main ()
{
saving sa;
creditCard cca;
checking ca;
string n;
int option;
int exit = 1;
int x = 1;
do{
cout << endl;
cout << "Checking Balance:" << ca.getBalance() << " " << "Savings balance:" << sa.getBalance() << " " << "Credit Card balance:" << cca.getBalance() << endl;
cout << endl;
cout << " (1) Savings Deposit " << endl;
cout << " (2) Savings withdrawel " << endl;
cout << " (3) Checking Deposit " << endl;
cout << " (4) Write A Check " << endl;
cout << " (5) Credit Card Payment " << endl;
cout << " (6) Make A Charge " << endl;
cout << " (7) Display Savings " << endl;
cout << " (8) Display Checkings " << endl;
cout << " (9) Display Credit Card " << endl;
cout << " (0) Exit " << endl;
cin >> option;
switch ( option )
{
case 1 : {
double SamtD;
cout << " Please enter how much you would like to deposit into savings " << endl;
cin >> SamtD;
sa.makeDeposit(SamtD);
break;
};
case 2 : {
int SamtW;
cout << " Please enter how much you would like to withdraw "<< endl;
cin >> SamtW;
sa.doWithdraw(SamtW);
break;
}
case 3 : {
double CamtD;
cout << " Please enter how much you would like to deposit into checkings " << endl;
cin >> CamtD;
ca.makeDeposit(CamtD);
break;
}
case 4 : {
double CamtW;
int chkNum;
cout << " Please enter how much you wrote on the check " << endl;
cin >> CamtW;
cout << " Please enter the check number " << endl;
cin >> chkNum;
ca.writeCheck(chkNum, CamtW);
break;
}
case 5 : {
double CCmkP;
cout << " Please enter the amount you would like to deposit " << endl;
cin >> CCmkP;
cca.makePayment(CCmkP);
break;
}
case 6 : {
double DoC;
string Nm;
cout << " Please enter the amount charged to your credit card " << endl;
cin >> DoC;
cout << " Please enter where the charge was made " << endl;
cin >> Nm;
getline(cin, Nm);
cca.doCharge(Nm,DoC);
break;
}
case 7 : {
sa.display();
break;
}
case 8 : {
ca.display();
break;
}
case 9 : {
cca.display();
break;
}
case 0 : exit = 0;
break;
default : exit = 0;
cout << " ERROR ";
}
}
while(exit==1);
return 0;
}
和这里是平衡正在被设置:
double curbalance = 100;
void account::setBalanceD(double balance) // This is for deposit
{
itsBalance = balance;
}
void account::setBalanceW(double balance) // This is for withdraw
{
double newBalance = curBalance - balance;
itsBalance = newBalance;
}
double account::getBalance()
{
return itsBalance;
}
下面是菜单选项2调用的代码:
int saving:: doWithdraw(int amount)
{
if (amount > 0)
{
for (int i = 9; i != 0; i--)
{
last10withdraws[i] = last10withdraws[i-1];
}
last10withdraws[0] = amount;
setBalanceW(amount);
}
else
{
cout << " ERROR. Number must be greater then zero. " << endl;
}
return 0;
}
任何想法?我只是无法保持平衡的准确性。
问题是您的setBalanceW()
函数从不从curBalance
中扣除,因此它停留在100
。
void account::setBalanceW(double balance) // This is for withdraw
{
curBalance -= balance;
}
你的其他参考也应该是curBalance
,因为应该只有一个当前余额。
void account::setBalanceD(double balance) // This is for deposit
{
curBalance += balance;
}
double account::getBalance()
{
return curBalance;
}
最好的解决方案是(假设itsBalance
是account
类的成员)完全消除curBalance
,只使用初始存款打开main()
:
int main ()
{
saving sa;
creditCard cca;
checking ca;
sa.makeDeposit(100.0);
/* other code */
}
/* Note no curBalance variable? */
void account::setBalanceW(double balance) // This is for withdraw
{
itsBalance -= balance;
}
void account::setBalanceD(double balance) // This is for deposit
{
itsBalance += balance;
}
double account::getBalance()
{
return itsBalance;
}
您执行存款的实现似乎不正确。应该是:
void account::setBalanceD(double balance) // This is for deposit
{
itsBalance += balance;
}
注意,我们现在将存入的金额添加到当前余额中,而不是显式地设置它的值。同样的,正如Ken指出的,这同样适用于执行取款。
void account::setBalanceW(double balance) // This is for withdraw
{
itsBalance -= balance;
}
相关文章:
- 在java中解决这段代码时面临循环中的问题
- 修改 VS Code 中的默认C++代码段
- 我是如何在这段代码中出现分段错误的
- 我不明白这段代码是如何对这个pythonlist()进行排序的,也不明白如何用C++中的向量来重现它
- 为什么这段代码不起作用,我该如何解决?
- 为什么这段代码给我错误? 有没有自错?
- 有人可以解释一下这段代码如何能够反转字符串
- 可能我知道为什么这段代码没有给出任何输出吗?
- 这段代码的最后一行在做什么?
- 我不知道为什么这段代码会让核心被转储?
- 我试图用这段代码找到二叉树的高度,但它一直返回 0,有人可以告诉我为什么吗?
- 有人可以向我解释一下这段代码的作用吗?
- 存储在哪个内存段(代码/数据段)类(员工)中?
- 为什么这段代码会导致无限循环?
- 为什么代码段会引发浮点异常?
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 如何为一段代码启用 -permissive
- 如何知道用于实现标准代码段的确切数据结构和算法,例如在C++STL中?
- 为什么下面的代码段返回指针指向的值而不是指针的地址?
- 谁能告诉我为什么这段代码没有产生正确的输出?