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