2011-03-13 44 views
1

在我的「computeInterest()」函數內,我需要一個循環來顯示用於從「enterAccountData()」之前填充數組的帳號,我會使用嵌套循環來查看帳號?或只是一個循環?類的循環

感謝您提前提供任何幫助,這裏是我的代碼。

#include <iostream> 
#include <iomanip> 

using namespace std; 

class BankAccount 
{ 
private: 
int accountNum; 
double accountBal; 
static const double annualIntRate; 
public: 
double enterAccountData(int, double); 
void computeInterest(); 
void displayAccount(); 
}; 
//implementation section: 
const double BankAccount::annualIntRate = 0.03; 
double BankAccount::enterAccountData(int number, double balance) 
{ 
cout << setprecision(2) << fixed; 



cout << "Enter the account number " << endl; 
cin >> number; 
accountNum = number; 
while(number < 0 || number < 1000) 
{ 
    cout << "Account numbers cannot be negative or less than 1000 " << 
     "Enter a new account number: " << endl; 
    cin >> number; 
} 

cout << "Enter the account balance " << endl; 
cin >> balance; 
accountBal = balance; 
while(balance < 0) 
{ 
    cout << "Account balances cannot be negative. " << 
     "Enter a new account balance: " << endl; 
    cin >> balance; 
} 
return balance,number; 
} 
void BankAccount::computeInterest() 
{ 
const int MAX_ACCOUNTS = 10; 
const int MONTHS_IN_YEAR = 12; 
int months; 
double rate = 0; 
int counter = 0; 

cout << "How many months will the account be held for? "; 
cin >> months; 
counter = 0; 
do 
{ 
accountBal = accountBal * annualIntRate + accountBal; 
counter++; 
}while(months > counter); 
/*for(counter = 0; counter < MAX_ACCOUNTS; counter++) 
{ 
    cout << "Account # " << counter << "Balance is:$" << accountBal << endl; 
}*/ 

} 

int main() 
{ 
const int QUIT = 0; 
const int MAX_ACCOUNTS = 10; 
int counter; 
int input; 
int number = 0; 
double balance = 0; 

BankAccount accounts[MAX_ACCOUNTS]; 
//BankAccount display; 

counter = 0; 

do 
{ 
    accounts[counter].enterAccountData(number, balance); 
    cout << " Enter " << QUIT << " to stop, or press 1 to proceed."; 
    cin >> input; 
    counter++; 

}while(input != QUIT && counter != 10); 

for(counter = 0; counter < MAX_ACCOUNTS; counter++) 
{ 
    accounts[counter].computeInterest(); 
} 
system("pause"); 
return 0; 
} 

我注意到了一個for循環,我先前累了沒有工作。

回答

1

你不需要循環。即使使用循環,也始終打印相同的值。

爲什麼?

程序有 BankAccount的實例,意思是每個實例都有自己的類成員副本。

const int MAX_ACCOUNTS = 10; 
const int MONTHS_IN_YEAR = 12; // Both of these declared global 

而且在main(),如果需要顯示所有賬戶信息 -

for(int i = 0; i < MAX_ACCOUNTS; ++i) 
{ 
    accounts[i].displayAccount(); // Where in displayAccount definition is to display accountNum and accountBal 
} 

如果在BankAccount::ComputeInterest(),需要顯示賬戶信息 -

void BankAccount::ComputeInterest() 
{ 


    // ...... 

    std::cout << "\n Acc. Num:\t " << accountNum << "\t Acc. Bal:\t" << accountBal << "\n"; 

} 

BankAccount::ComputeInterest()沒有按沒有任何參照其他實例的參數。因此,它具有僅在其上被調用的對象信息。而且該計劃可以更好地組織起來。

有變量不必要的重複(即,MAX_ACCOUNTS等)

double BankAccount::enterAccountData(int number, double balance)返回類型是double

double BankAccount::enterAccountData(int number, double balance) 
{ 
    // .... 

    return balance,number; 
} 

return永遠不能返回多個值。最後一個變量的值被返回。所以,只返回number

+0

@Alex:如果您不介意提出建議,請閱讀本書。 http://www.amazon.com/dp/0201700735/?tag=stackoverfl08-20 – Mahesh 2011-03-13 21:16:28