Well I am trying to do an exercise in my programming book and it's difficult for me to grasp exactly what it wants.
My "enterAccountData()" function is suppose to ask the user for an account number and a balance neither of which can be negative and the account number cannot be less than 1000
The second one is the one I am stuck on "computeInterest()" THis function is suppose to accept an integer argument that represents the number of years the account will earn interest. The function then displays the account number from the previous function and displays its ending balance at the end of each year based on the interest rate attached to the BankAccount class. (The interest rate on "BankAccount" has to be a constant static field which is set at 3 percent(0.03)).
So My question is this: How do I set up "computeInterest()" too allow it to calculate the interest using the constant static field when my debugger will not allow me to actually keep the field as a constant? I am not trying to stop any random errors from happening for now, just trying to get the jist of what the book is exactly asking for. Here is my code.
#include <iostream> #include <iomanip> using namespace std; class BankAccount { private: int accountNum; double accountBal; static double annualIntRate; public: void enterAccountData(int, double); void computeInterest(); void displayAccount(); }; //implementation section: double BankAccount::annualIntRate = 0.03; void BankAccount::enterAccountData(int number, double balance) { cout << setprecision(2) << fixed; accountNum = number; accountBal = balance; cout << "Enter the account number " << endl; cin >> number; while(number < 0 || number < 999) { 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; while(balance < 0) { cout << "Account balances cannot be negative. " << "Enter a new account balance: " << endl; cin >> balance; } return; } void BankAccount::computeInterest() { const int MONTHS_IN_YEAR = 12; int months; double rate = 0; int counter = 0; BankAccount::annualIntRate = rate; cout << "How many months will the account be held for? "; cin >> months; counter = 0; do { balance = accountBal * rate + accountBal; counter++; }while(months < counter); cout << "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); accounts[counter].computeInterest(); system("pause"); return 0; }
while(number < 0 || number < 999)is exactly the same in effect aswhile(number < 999). It also seems like it should be using1000, not999, otherwise account # 999 will be acceptable.I = Prtfor interest rate calculation? Also note that you're only printing out one account's "computeInterest" since you're doing it at counter. Also, that account won't exist - tryaccounts[counter-1].computeInterest()