我一直在做这个模拟银行账户的项目。用户可以存款、取款,并在屏幕上显示所有取款和存款。在选择菜单的顶部需要是当前余额。就像这样,储蓄: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 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;
}
有任何想法吗?我就是无法让天平保持准确。