Loops with classes
Within my "computeInterest()" function I need a loop to display the account number that was used to populate the array earlier from "enterAccountData()" would I use a nested loop that would look at the account numbers? or just one loop?
Thanks for any help in advance and here is my code.
#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 " << QUI开发者_运维技巧T << " 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;
}
I commented out a for loop that I tired earlier that didn't work.
You don't require a loop. Even if a loop is used, always same value gets printed.
WHY ?
The program has 10 instances of BankAccount
, meaning each having it's own copy of class members.
const int MAX_ACCOUNTS = 10;
const int MONTHS_IN_YEAR = 12; // Both of these declared global
And in main()
, if you need to display all the accounts information -
for(int i = 0; i < MAX_ACCOUNTS; ++i)
{
accounts[i].displayAccount(); // Where in displayAccount definition is to display accountNum and accountBal
}
And if in the BankAccount::ComputeInterest()
, need to display the account information -
void BankAccount::ComputeInterest()
{
// ......
std::cout << "\n Acc. Num:\t " << accountNum << "\t Acc. Bal:\t" << accountBal << "\n";
}
BankAccount::ComputeInterest()
doesn't take any argument that has reference to other instances. So, it has object information on which it is called only. And the program can be even better organized.
There are unnecessary repetitions of variables ( i.e., MAX_ACCOUNTS etc.,)
And double BankAccount::enterAccountData(int number, double balance)
return type is double
.
double BankAccount::enterAccountData(int number, double balance)
{
// ....
return balance,number;
}
return
can never return more than one value. The last most variable's value is returned. So, number
is only returned.
精彩评论