gpt4 book ai didi

c++ - 无法弄清楚如何执行此代码

转载 作者:行者123 更新时间:2023-11-28 07:39:49 25 4
gpt4 key购买 nike

我一直在从事这个模拟银行账户的项目。用户可以存款、取款,并在屏幕上显示所有的取款和存款。选择菜单的顶部需要是当前余额。像这样,储蓄: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 储蓄::doWithdraw(int 金额)

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

不过,最好的解决方案(假设 itsBalanceaccount 类的成员,将完全消除 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;
}

关于c++ - 无法弄清楚如何执行此代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073213/

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