开发者

Passing an array of objects, the count of objects and a desired term to a function

开发者 https://www.devze.com 2023-03-05 00:34 出处:网络
I\'ve run into some trouble in my programming book while completing an exercise to do with BankAccounts. The question asks:

I've run into some trouble in my programming book while completing an exercise to do with BankAccounts. The question asks:

Create a main() function that declares an array of 10 BankAccount objects. After the first BankAccount object is entered, ask the user if he or she wants to continue. Prompt the user for BankAccount values until the user wants to quit or enters 10 BankAccount objects, whichever comes first. For each BankAccount entered, display the BankAccount details. Then prompt the user for a term with a value between 1 and 40 inclusive. Continue to prompt the user until a valid value is entered. Then pass the array of BankAccount objects, the count of valid objects in the array, and the desired term to a function that calculates and displays the values of each of the BankAccounts after the given number of years at the standard interest rate.

I have to use 3 public functions to complete this task. I only start running into problems when I try to computeInterest(). If I attempt to pass the term and count to the function and set it up in 2 for loops I will only receive the numbers and balances for a single object. I guess what i'm asking is: How do I get the program to call computeInterest and run through for each BankAccount object?

At the moment my code is set so that it will compile and run perfectly well, however, the question asks for the term and count to be sent to the function while I'm only sending the term.

I'm not sure if I'm sending the array of objects to the function? Which may be whats causing the trouble. This has been grinding my brain for quite a few days now :(. Any help/pointers are greatly appreciated!

#include<iostream>
#include<string>
using namespace std;

// Declaration Section

class BankAccount
{
private:
    int accNum;
    double accBal;
    const static double intRate;

public:
    void enterAccountData(int, double);
    void computeInterest(int);
    void displayAccount();
};

// Implementation Section

const double BankAccount::intRate = 0.03;

void BankAccount::enterAccountData(int num, double bal)
{
    const int MIN_ACC = 1000, MAX_ACC = 9999, MIN_BAL = 0,
    MAX_BAL = 100000;

    cout << "Enter account number: ";
    cin >> num;

    while (num < MIN_ACC || num > MAX_ACC)
    {
        cout << "ERROR: Account number must be between " << MIN_ACC <<
        " - " << MAX_ACC << ".\n" << "Enter account number: ";
        cin >> num;
    }

    cout << "Enter account balance: $";
    cin >> bal;

    while (bal < MIN_BAL || bal > MAX_BAL)
    {
        cout << "ERROR: Account balance must be positive and less than $" <<
        MAX_BAL << ".\n" << "Enter account balance: $";
        cin >> bal;
    }

    accNum = num;
    accBal = bal;
}

void BankAccount::computeInterest(int YR)
{
    int x;
    double interest;

    for (x = 0; x < YR; ++x)
    {
        interest = accBal * intRate;
        accBal = accBal + interest;

        cout << "Account# " << accNum <<
            ", Balance: $" << accBal << ", Interest: " <<
            intRate << endl;
    }
    cout << endl;
}

void BankAccount::displayAccount()
{
    cout<<"Account: " << accNum <<", Balance: $" << accBal <<
    ", Interest: " << intRate << endl << endl;
}

int main()
{
    const int END = -999, MAX_ACCOUNTS = 10, CONTINUE = 1, MIN_TERM = 1,
    MAX_TERM = 40;
    int i, x, count = 0, num = 0, term = 0;
    double bal = 0;

    BankAccount accounts[MAX_ACCOUNTS];

    do
    {
        count++;
        accounts[count - 1].enterAccountData(num, bal);
        accounts[count - 1].displayAccount();
        cout << "Enter " << CONTINUE << " to proceed, or " << END << " to stop: ";
        cin >> i;

        if (i == END || count == MAX_ACCOUNTS)
        {
            cout << "Enter the term of the accounts (" << MIN_TERM <<
                "-" << MAX_TERM << "): ";
            cin >> term;

 开发者_如何学编程           while (term < MIN_TERM || term > MAX_TERM)
            {
                cout << "ERROR: Term must be between " << MIN_TERM <<
                    " - " << MAX_TERM << " inclusive.\n" <<
                    "Enter term of the accounts (" << MIN_TERM <<
                    "-" << MAX_TERM << "): ";
                cin >> term;
            }

            for (x = 0; x < count; ++x)
                accounts[x].computeInterest(term);
        }
    } while (i != END && count != MAX_ACCOUNTS);
}


You are already doing it in for (x = 0; x < count; ++x) accounts[x].computeInterest(term);

If really necessary, you can move that particular piece of code in a separate function, one that accepts 3 parameters: the array accounts, the count and the term.

The way you have setup the BankAccount class is that each object calculates its own interest. Since you have several such objects, you can ask each one to compute its own interest.

Hope this helps!Let me know if there are any other questions :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号