0

I am having some trouble When I run my current program. I can add any number of transactions within this account balance, but when I go past the first array value and enter information, it displays a memory location.. Whats my problem in my code then here.

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

The source code for this is here:

//Alex Weir
// 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+=(BankAccount);
int operator+(BankAccount);
void displayAccounts();
};
double BankAccount::operator+=(BankAccount account)
{
cout << "How much money would you like to deposit or withdraw?" << endl <<
    " Enter a negative amount to withdraw." << endl;
cin >> debitCredit;
debitCredit = debitCredit + account.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;
}
}
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+=(accounts[x]);
        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)
{

}*/

Okay now that I have gotten rid of x I continue to have my variable as "i" now, I go through the 5 array elements, but I want to start with array 0 then go through the loop as many times as I want to (still playing with the balance at array element 0) after I hit "stop" or 0 when given the opportunity I want to move onto the 1st array element and go through it, adding and sub tracting for as much as I feel nessacary and repeating this process until I am fine with it. re setting the array's element variable to "i" does not do this and carries over from the last array element used. system("pause"); return 0; }

3
  • 6
    The point of overloading operator+= was so you could just type +=... Commented Mar 29, 2011 at 1:20
  • Is the variable x initialized anywhere? Commented Mar 29, 2011 at 1:23
  • I added my source code for it to be simpler to understand. Commented Mar 29, 2011 at 1:32

1 Answer 1

2

There are several things that might be wrong here:

debitCredit = accounts[x].operator+=(accounts[x]);
  1. Why don't you use i instead of x? Is the value of x even initialized (the code doesn't show)?
  2. Why do you add accounts[x] to itself? In effect you are doubling its value.
  3. Is the accounts array big enough? How is it initialized?

Update

After looking at the extra code you posted: lose the x and use i instead. You are overstepping the bounds of the accounts array.

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

5 Comments

After his edit, the answer to his original question is clearly your #1, but for good measure allow me to add doing I/O inside an operator, using a double to store monetary values, and inconsistent indentation to your list.
@KarlBielefeldt: You were just faster than me, I just checked for an update and updated the answer. As for the list... we could be here all night adding things.
I wish my bank did #2, so that each account statment showed twice the amount of the previous one!
The think is it doesn't show twice the previous, just the previously inputted deposit or balance.... and it never carries over.
Fixed. Thanks for everyone's feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.