1

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; } 
7
  • I used months because that seemed...easier to me because this way it goes through every month, 12 months = 1 year and so on. Commented Mar 13, 2011 at 4:16
  • 1
    If you are doing it in months you should divide the rate by 12 first because interest rates are typically defined as interest earned for a year. Commented Mar 13, 2011 at 4:23
  • This line: while(number < 0 || number < 999) is exactly the same in effect as while(number < 999). It also seems like it should be using 1000, not 999, otherwise account # 999 will be acceptable. Commented Mar 13, 2011 at 4:28
  • Why not use I = Prt for 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 - try accounts[counter-1].computeInterest() Commented Mar 13, 2011 at 4:29
  • @Rage: I = Prt? Pay * rate * time? @Ted Hopp:The account number cannot be negative and thats why I put that piece in the loop. @Silence: I have to display at years end the new account balance, so if I just go by months it would compute each month then I would add a loop that holds the year and display the information. Commented Mar 13, 2011 at 4:40

2 Answers 2

2

The constant field is easy enough:

class BankAccount { ... const static double annualIntRate = 0.03; ... } 

(Does your debugger complain about that? I'm using gcc 4.2.1) But there are other troubling things in your code, like the way computeInterest tries to set rate to zero, and the while loop... needs work.

EDIT:
One good principle is worth a hundred specific corrections. When you develop a complicated function, don't try to do it all at once; start with a simple piece, get that working perfectly, then build up. F'rinstance, computeInterest. You have several independent parts to get working: going through the while loop the correct number of times, calculating interest increments, keeping track of the balance-- right now computeInterest does none of these correctly. Tackle them one at a time, or in parallel if you want, but never combine parts that don't work.

Sign up to request clarification or add additional context in comments.

1 Comment

Well I got the static field to work. on what about the while loop needs work? Oh and I found a link to the exact question that I am trying to complete. here is a link books.google.com/…
1

boy it's been a LONG time since I've worked in C++, but I think all you have to do is this:

static double annualIntRate =.03; 

in the "private" section of your code.

and then you can use annualIntRate as though it was a global (to each instance of the class) variable.

3 Comments

I changed it and it gave me some errors which I fixed. It now wants me to show that I am initializing "balance" within computeInterest, and I don't understand how to do this in classes. It also is giving an error that I cannot figure out "Error 1 error C2864: 'BankAccount::annualIntRate' : only static const integral data members can be initialized within a class."
well, that means my memory of C++ is ill informed. you can't instatiate variables in that way. I apologize for the misleading answer.
Its okay, this book is just really not very helpful and I could not find much about Object oriented programming online that is relate-able to my question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.