Hello everyone,

I am having some problems with my code and allowing it to do what I want.
I need within main to have the account balance from my class to be allowed to change value through "transactions" adding negative numbers for a withdraw and positive numbers to put in a deposit within the accounts balance. I want to do this an unlimited amount of times and I cannot seem to get the loop for it to work correctly.

Here is my code: The problem happens near the bottom.

// Case 2 Chapter 9
#include <iostream>
#include <iomanip>

using namespace std;

class BankAccount
{

friend ostream& operator<<(ostream&, const BankAccount&);
friend istream& operator>>(istream&, BankAccount&);
private:
	int accountNum;
	int increaseAccountNum;
	double accountBal;
	double annualIntRate;
	double debitCredit;
public:
	BankAccount();
	BankAccount(int,int,double,double,double);
	int operator==(const BankAccount&);
	void operator<(const BankAccount&);
	void operator>(const BankAccount&);
	double operator+=(double debitCredit);
	int operator+(BankAccount);
	void displayAccounts();
};
double BankAccount::operator+=(double debitCredit)
{
	cout << "How much money would you like to deposit or withdraw?" << endl <<
		" Enter a negative amount to withdraw." << endl;
	cin >> debitCredit;
	debitCredit = debitCredit + accountBal;
	return debitCredit;
}
int BankAccount::operator+(BankAccount account)
{
	int increment;
	int accountNum = increment + account.accountNum;
	return accountNum;
}
void BankAccount::operator>(const BankAccount& accounts)
{

	if(accountBal > accounts.accountBal)
	{
		cout << "Account Balance is greater than another account." << endl;
	}
	else
	{
		cout << "Account Balance is less than another account." << endl;
	}
}
void  BankAccount::operator<(const BankAccount& accounts)
{
	if(accountBal < accounts.accountBal)
	{
		cout << "Account Balance is less than another account." << endl;
	}
	else
	{
		cout << "Account Balance is greater than another account." << endl;
	}
}
int BankAccount::operator==(const BankAccount& acctNumb)
{
	int isTrue = 0;
	if(accountNum == acctNumb.accountNum)
		isTrue = 1;
	return isTrue;
}
ostream& operator << (ostream& out, const BankAccount& Accounts)
{
	cout << endl;
	out << "Account #" << Accounts.accountNum << endl;
	out << "Account Balance:$" << Accounts.accountBal << endl;
	out	<< "Account interest rate: " << Accounts.annualIntRate << endl;
	cout << endl;
	return out;
}
istream& operator >> (istream& in, BankAccount& Accounts)
{
	cout << "Enter Account # " << endl;
	in >> Accounts.accountNum;
	cout << "Enter Account Balance: $";
	in >> Accounts.accountBal;
	cout << endl << "Enter Account Interest Rate: " << endl;
	in >> Accounts.annualIntRate;
	cout << endl;
	return in;
}
BankAccount::BankAccount()
{
	accountNum = 0;
	accountBal = 0;
	annualIntRate = 0;
	increaseAccountNum = 0;
	debitCredit = 0;
}
BankAccount::BankAccount(int acctNum, int increment, double acctBal, double intRate, double debCred)
{
	accountNum = acctNum;
	accountBal = acctBal;
	annualIntRate = intRate;
	increaseAccountNum = increment;
	debitCredit = debCred;
}
void BankAccount::displayAccounts()
{
	cout << "Account # " << accountNum << endl;
	cout << "Account balance:$" << accountBal << endl;
	cout << "Account Interest Rate: " << annualIntRate << endl;
	cout << endl;
}
int main()
{
	const int ACCTS = 5;
	const int QUIT = 0;
	int x, selection;
	double debitCredit = 0.0;
	BankAccount accounts[ACCTS];

	cout << "Enter Bank account information for: " << ACCTS << " accounts." << endl; 

	for(x = 0; x < ACCTS; ++x)
	{
		accounts[x].displayAccounts();
	}
	for(int i = 0; i < ACCTS; ++i)
	{
		do
		{
			debitCredit = accounts[x].operator+=(debitCredit);
			cout << "Account Balance is:$" << debitCredit << endl;
			cout << "Enter " << QUIT << " to stop transactions." << endl;
			cin >> selection;
		}while(selection != QUIT);
		x++;
	}
	for(x = 0; x < ACCTS; ++x)
	{
		accounts[x].displayAccounts();
	}
	/*for(x = 0; x < ACCTS; ++x)
	{
		cout << "Entry #" << (x + 1) << endl;
		cin >> accounts[x];
		cout << accounts[x];
	}
	double transactions;
	for(x = 0; x < ACCTS; ++x)
	{
		
	}*/
	

	system("pause");
	return 0;
}

Edited 5 Years Ago by Vindal: Took out my real name.

The commented part is just extra code that I will be adding in later.

I'm not sure exactly which loop you're looking at here. Basically the whole main function? If so, then you could use a do while loop and have the condition set to a user-inputted variable. If you're talking about the for loop that continues until x > ACCTS, you've set ACCTS as a constant 5, so the loop will not run infinitely, but after 5 iterations... I may not be understanding you properly, if that's the case please try to be a little more descriptive.

Okay so, when I set it up I can input as many transactions and I want it goes over five and even over 50 if I want, but anytime I enter 0 it goes to the next array element and it should take in new information and display that information as I add and withdraw until I enter 0 again. This means that the for loop doesn't add 1 to the array element until after I enter 0.

Hi I am sorry for all the back and forth . But can you please clearly state what is the output that you want ? And what is the current output that you are getting . Please be as descriptive as possible

This article has been dead for over six months. Start a new discussion instead.