2014-03-05 186 views
0
const int CHECK_MAX = 50; 

void balanceAccount(double account) 
{ 
    double start = 0; 
    double interest = 0; 
    double check[CHECK_MAX]; 

    account = (start - check[CHECK_MAX]) * (1 + interest); 
    cout << account; 
} 

int main() 
{ 
    int sub; 
    double start = 0; 
    double interest = 0; 
    double total = 0; 
    double check[CHECK_MAX]; 
    void balanceAccount(double); 

    cin >> start; 

    for (sub = 0; sub < CHECK_MAX; sub++) 
    { 
     cin >> check[sub]; 

     if (check[sub] == 0) 
     { 
      cout << "Thank you." << endl; 
      break; 
     } 
    } 

    cin >> interest; 

    if (interest == 0) 
    { 
     balanceAccount(total); 
    } 
     else 
    { 
      balanceAccount(total); 
    } 

    return 0; 
} 

該程序的目的是提示用戶輸入銀行賬戶的起始餘額,然後使用數組,輸入個人金額的支票(最多50),並有他們從初始餘額中扣除。然後,如果存在利息值,則乘以總值。最終結果是使用balanceAccount()函數計算的。我相信在最後的計算之前,所有的東西都能正常工作。我一直得到不正確的計算。我最終將包括一個貨幣格式,但現在我只是想讓它正確計算。感謝您的時間。函數不返回正確的結果

+3

爲什麼你是否有一個'if ... else'在結尾做同樣的事情? –

+2

在這麼多層次上有這麼多的錯誤。 'total'在'main'中沒有用處,'interest'在'balanceAccount()'中沒有用處。 – Neoh

+0

你說得對嗎? –

回答

1
double check[CHECK_MAX]; 

account = (start - check[CHECK_MAX]) * (1 + interest); 

訪問check[CHECK_MAX]無效 - 它在陣列末尾運行。 C++數組的索引爲0-n-1,因此數組中的最後一項是check[CHECK_MAX-1]

0

balanceAccount()中的所有變量都被初始化爲零,因此main()中的其他變量不起作用。爲了解決這個問題,通過變量balanceAccount()

void balanceAccount(double localAccount, double localStart, double localInterest, double localCheck[]) 
{ 
    account = (start - check[CHECK_MAX]) * (1 + interest); 
    cout << account; 
} 

你也可能要返回account「。

這條線:

account = (start - check[CHECK_MAX]) * (1 + interest); 

去過去的數組的末尾,它應該是:

account = (start - check[CHECK_MAX-1]) * (1 + interest); 

我也不知道你是在結束與if...else做什麼main()

0

變化balanceAccount()如下方法..

double balanceAccount(char account[]) 
{ 
    double start = 0; 
    double interest = 0; 
    double check[CHECK_MAX]; 

    double account = (start - check[CHECK_MAX-1]) * (1 + interest); 
    cout << account; 
} 

對於主balanceAccount()

total = balanceAccount(check);

原因在balanceAccount()傳遞check;你在main()balanceAccount()創建不同check的並且兩者不相同...

而且check[CHECK_MAX]操作無效...... 0 to CHECK_MAX-1是,其對高達CHECK_MAX有效的存儲位置......在上述

0

工作代碼的代碼基於您的發言嘗試這種改變根據,

#include<iostream> 
using namespace std; 

const int CHECK_MAX = 50; 

void balanceAccount(double total,double interest) 
{ 
    double account = (total) * (interest); 
    cout << account; 
} 

int main() 
{ 
    int sub; 
    double start = 0; 
    double interest = 0; 
    double total = 0; 
    double check[CHECK_MAX]; 
    void balanceAccount(double,double); 

    cout << "Enter the beginning balance of the account at the beginning of the month." << endl; 
    cin >> start; 
    total = start; 
    cout << "Enter the individual dollar amount of checks written this month." << endl; 
    cout << "A maximum number of 50 checks is enforced." << endl; 
    cout << "Enter a zero when you are finished entering check values." << endl; 
    for (sub = 0; sub < CHECK_MAX; sub++) 
    { 
     cout << "Enter the dollar amount for the checks written, but one at a time." << endl; 
      cin >> check[sub]; 
     total = total - check[sub]; 
     if (check[sub] == 0) 
     { 
      cout << "Thank you." << endl; 
      break; 
     } 
    } 

    cout << "If applicable, enter an interest rate." << endl; 
    cout << "If there is no interest rate, enter a 0." << endl; 
    cin >> interest; 
    if(interest != 0) 
    { 
     cout << "Thank you." << endl; 
     balanceAccount(total,interest); 
    } 
    else 
    { 
     cout << "Thank you." << endl; 
     cout << total << endl; 
    } 
    return 0; 
} 
+0

@安德魯你是否試過這個解決方案。 –

+0

是的,這工作完美。謝謝!我對C++和編程非常陌生。這有很多幫助。 –

+0

很好聽@AndrewWilson –