0

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

有任何想法吗?我就是无法让天平保持准确。

4

2 回答 2

1

问题是你的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;
}
于 2013-04-18T02:00:30.613 回答
0

您执行存款的实施似乎不正确。它应该是:

void account::setBalanceD(double balance) // This is for deposit
{
    itsBalance += balance;
}

请注意,我们现在将存入的金额添加到当前余额中,而不是显式设置它的值。与 Ken 指出的类似,这同样适用于执行撤回。

void account::setBalanceW(double balance) // This is for withdraw
{
    itsBalance -= balance;
}
于 2013-04-18T01:58:06.320 回答