c++ - Passing an array of objects, the count of objects and a desired term to a function -
i've run trouble in programming book while completing exercise bankaccounts. question asks:
create main() function declares array of 10 bankaccount objects. after first bankaccount object entered, ask user if or wants continue. prompt user bankaccount values until user wants quit or enters 10 bankaccount objects, whichever comes first. each bankaccount entered, display bankaccount details. prompt user term value between 1 , 40 inclusive. continue prompt user until valid value entered. then pass array of bankaccount objects, count of valid objects in array, , desired term function calculates , displays values of each of bankaccounts after given number of years @ standard interest rate.
i have use 3 public functions complete task. start running problems when try computeinterest(). if attempt pass term , count function , set in 2 loops receive numbers , balances single object. guess i'm asking is: how program call computeinterest , run through each bankaccount object?
at moment code set compile , run well, however, question asks term , count sent function while i'm sending term.
i'm not sure if i'm sending array of objects function? may whats causing trouble. has been grinding brain quite few days :(. help/pointers 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 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 positive , less $" << max_bal << ".\n" << "enter account balance: $"; cin >> bal; } accnum = num; accbal = bal; } void bankaccount::computeinterest(int yr) { int x; double interest; (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]; { count++; accounts[count - 1].enteraccountdata(num, bal); accounts[count - 1].displayaccount(); cout << "enter " << continue << " proceed, or " << end << " stop: "; cin >> i; if (i == end || count == max_accounts) { cout << "enter term of accounts (" << min_term << "-" << max_term << "): "; cin >> term; while (term < min_term || term > max_term) { cout << "error: term must between " << min_term << " - " << max_term << " inclusive.\n" << "enter term of accounts (" << min_term << "-" << max_term << "): "; cin >> term; } (x = 0; x < count; ++x) accounts[x].computeinterest(term); } } while (i != end && count != max_accounts); }
you doing in for (x = 0; x < count; ++x) accounts[x].computeinterest(term);
if necessary, can move particular piece of code in separate function, 1 accepts 3 parameters: array accounts
, count
, term
.
the way have setup bankaccount class each object calculates own interest. since have several such objects, can ask each 1 compute own interest.
hope helps!let me know if there other questions :)
Comments
Post a Comment