Hello,

I am having some troubles with coding from one of Joyce Farrell's books here is a link showing the exact thing to which I am trying to figure out. Exact question

Here is the code I have written thus far.

#include <iostream>
#include <iomanip>

using namespace std;

class BankAccount
{
private:
	int accountNum;
	double accountBal;
	static const double annualIntRate = 0.03;
public:
	void enterAccountData(int, double);
	void computeInterest();
	void displayAccount();
};
 //implementation section:
const 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;

	cout << "How many months will the account be held for? ";
	cin >> months;
	counter = 0;
	do
	{
	 balance = balance * annualIntRate + balance;
	 counter++;
	}while(months < counter);
	cout << "Balance is:$" << balance << 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-1].computeInterest();
	
	system("pause");
	return 0;
}

Currently this has 5 Errors, I am not sure how to work with the static constant that is asked from "computeInterest()" Classes are new to me and I had been writing structures up until this chapter, so it threw me for a loop. This also causes me to have the error of not knowing how to bring the balance from my one function and compute it within another (I also need to display the account number within the function.)

Thanks for any help in advance.

Recommended Answers

All 10 Replies

Remove the =0.03 from the declaration (line 11).

balance is a parameter of enterAccountData(), so it goes out of scope once that method ends. Use accountBal in computeInterest() instead.

In the first place, you cannot assign a const value in the class header. Second, you didn't declare the variable balance in the void computeInterest() function

Okay I applied what Jonsca gave me and my program runs (and there was much rejoicing.) Now the balance is not computed correctly. Do I use a pointer from the "enterAccountData()" function that would then be related to the array to which the balance is placed?

Actually, you don't use the balance you are passing in anyway. Remove that parameter. Assign it to accountBal *after* the user enters it in. Now the object will carry the accountBal around (and you can access it from all of the other methods).

commented: Helpful and understanding. +1

Sorry the wording has me confused, would you mind giving me an example?

like this you mean:?

cin >> balance;

accountBal = balance?

Yes, that's exactly it. The order in which you were doing it before was writing 0 to the variable (that you had passed in), taking input to write over the variable you had passed in. accountBal wasn't getting anything but the 0.

Fantastic! It works! only thing now though is that it only does the process once.....not 12 times like I tell it to....my while loop must be messed up : /

I just changed the sign of the less than to greater than and it computes for the whole year; hurray.

but it does repeat itself within my "computeInterest()" function is there a way to make it look at only the array's that are populated? The program asks for you to allow the user to input up to 10 accounts so if its less than 10 the memory address will be looked at only and I don't want that.

You've got months and counter flipped around. Count up until you reach months, not the other way around.

You've got months and counter flipped around. Count up until you reach months, not the other way around.

I'm not sure I fixed it exactly : / Here's my new code though.

#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 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);
	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);

	for(counter = 0; counter < MAX_ACCOUNTS; counter++)
	{
		accounts[counter].computeInterest();
	}
	system("pause");
	return 0;
}

just a little bit more and I can work on the next part of the exercise.

while(counter < months) I meant, sorry.
counter = 0, counter < months = true, loop continues
counter = 1, counter < months = true, loop continues
...
counter = months, counter < months = false, loop stops.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.