Alex
Alex

Reputation: 113

Overloading operators..getting a logical error

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; }

Upvotes: 0

Views: 238

Answers (1)

Jon
Jon

Reputation: 437336

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.

Upvotes: 2

Related Questions