2013-04-18 40 views
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量)

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

任何想法?我無法保持平衡以保持準確。

+3

永遠不要使用彩車或雙打代表錢。他們是天生不準確的。 – Cairnarvon

回答

0

您的執行存款執行似乎是不正確的。它應該是:

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

請注意,我們現在將存入的金額添加到當前餘額中,而不是明確地設置它的值。 Ken也指出,同樣適用於退出。

void account::setBalanceW(double balance) // This is for withdraw 
{ 
    itsBalance -= balance; 
} 
+0

我想'setBalanceW'實際上是問題,因爲它使用了一個外部變量,而不是基於當前平衡可言。 –

+0

謝謝這就是完美! – Dolbyover

+0

他們'再bo th bolloxed。 –

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; 
} 
+0

謝謝你這完美! – Dolbyover