I need to be able to loop and get information for up to 10 "BankAccount" arrays, but when I use less than 10 it gives me garbage and I don't know how to fix it.

Here is my Code.

#include <iostream>
#include <iomanip>

using namespace std;

class BankAccount
{
private:
	int accountNum;
	double accountBal;
	static const double annualIntRate;
public:
	void enterAccountData();
	void computeInterest();
	void displayAccount();
};
 //implementation section:
const double BankAccount::annualIntRate = 0.03;
void BankAccount::enterAccountData()
{
	cout << setprecision(2) << fixed;
	
	

	cout << "Enter the account number " << endl;
	cin >> accountNum;

	while(accountNum < 1000)
	{
		cout << "Account numbers cannot be negative or less than 1000 " <<
			"Enter a new account number: " << endl;
		cin >> accountNum;
	}

	cout << "Enter the account balance " << endl;
	cin >> accountBal;

	while(accountBal < 0)
	{
		cout << "Account balances cannot be negative. " <<
			"Enter a new account balance: " << endl;
		cin >> accountBal;
	}
}
void BankAccount::computeInterest()
{
	const int MAX_ACCOUNTS = 10;
	const int MONTHS_IN_YEAR = 12;
	int months = 0;
	int years = 0;
	double rate = 0; 
	int count = 0;

	cout << endl << "How many years will the account be held for? (1 to 40 years) " << endl;
	cin >> years;
	months = years * 12;
	count = 0;
	while(years < 1 || years > 40)
	{
		cout << "Term must be from 1 to 40." << endl;
		cin >> years;
	}
	do
	{
	 accountBal = accountBal * annualIntRate + accountBal;
	 count++;
	 if(count == 12)
	 {
		cout << endl << "The balance after " << months << " months is " << endl;
	 }
	}while(count < months);
	
	cout << "$" << accountBal << " which is for account # " << accountNum << 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();
		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;
}

Even if you set MAX_ACCOUNTS to be 5, your do while loop still checks that counter is less than the integer 10 rather than MAX_ACCOUNTS, assuming that 1 is continually entered after counter surpasses MAX_ACCOUNTS.

You'd be pretty much trying to use an object that you never created, is what I think the problem is.

So your saying don't check it too MAX_ACCOUNTS, but check it with some other number that would be how ever many objects was created by the user?

Or, you can create a constructor for your class, so that it initializes the class's members so that they don't store garbage.

You then call the constructor when declaring your class array.

Or, as you are doing it, instead of

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

use a new variable for the if loop and use the counter as a sentinel:

for(int i = 0; i < counter; i++)
{
accounts[i].computeInterest();
}

Since you are counting into counter each time an account gets data entered, you can use it as the sentinel for your print loop

Oh, I'm actually saying you should check it to that. You've specified that MAX_ACCOUNTS is the amount you'd like the user to have, but elsewhere you've used a constant value i.e. 10 rather than the variable MAX_ACCOUNTS.

Your counter variable should be comparing itself to the amount you specified when you declared your array of objects. Still, you'd have a problem if the user exits before each object is given values. If you're specifying the amount of objects you'd want the user to utilize, then you could always remove the input test and have them read in data for MAX_ACCOUNTS amount of objects.

I apologize if I'm a bit wordy with this.

I guess what Chilton is trying to say is that you should compare your do... while loop to MAX_ACCOUNTS, instead of the constant number 10.
So, instead of this

do
{
... 
}while(input != QUIT && counter < 10);

Do this:

do
{
... 
}while(input != QUIT && counter < MAX_ACCOUNTS);

That way you can change the constant value anytime, and you actually use the constant variable you created.

commented: Yup. +2
commented: Very Helpful +1

Thank you to both of you it works now. I don't get how a few lines of code that seemingly says the same thing can alter everything and get you to the results you want...Not sure I will ever be able to understand C++.

@Nichito

Constructors are the next chapter in my book im working off of : )

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.