无法弄清楚如何做这个代码

时间:2013-04-18 01:50:33

标签: c++

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

有什么想法吗?我无法保持准确的平衡。

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

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

请注意,我们现在正在将存入的金额添加到当前余额中,而不是明确设置其值。同样,Ken指出同样适用于退出。

void account::setBalanceW(double balance) // This is for withdraw
{
    itsBalance -= balance;
}
相关问题