Following function is from a bank account, when new account is created it won't store it in the array or i guess it does but find_acct function wont be able to find it. What is wrong with it? Please help me!!
find_acct Function:
1 2 3 4 5 6 7
int findacct(int acctnum_array[], int num_accts, int requested_account)
{
for (int index = 0; index < num_accts; index++)
if (acctnum_array[index] == requested_account)
return index;
return -1;
}
Is acctnum_array and balance_array large enough to hold num_accts? C-style arrays are not dynamic in size. I would suggest using another container such as a list or vector and using the push_back(...) member function to add new accounts. Also, you would probably benefit from making a class or struct, e.g.
struct BankAccount
{
// can take 1 argument (just account number, default balance is zero)
// or 2 arguments (account number and balance)
BankAccount(int acctnum, double bal = 0.0)
: account_number(acctnum), balance(bal)
{ }
// data members (public by default in struct)
int account_number;
double balance;
// comparison of bank account objects
booloperator==(const BankAccount& rhs) const
{ return (this->account_number == rhs.account_number); }
// use not of ==
booloperator!=(const BankAccount& rhs) const
{ return !(*this == rhs); }
};
1 2 3 4
// use a typedef for cleanness
typedef std::list<BankAccount> ACCOUNT_LIST;
// global list of accounts (should pass by reference to new_acct function)
ACCOUNT_LIST accounts;
1 2 3 4 5 6 7 8 9 10
ACCOUNT_LIST::iterator findacct(const BankAccount& acct, ACCOUNT_LIST& bankaccounts)
{
for(ACCOUNT_LIST::iterator ite = bankaccounts.begin(); ite != bankaccounts.end(); ++ite)
{
// return an iterator to account if exists
if(*ite == acct) { return ite; }
}
// return the end iterator if doesn't exist
return bankaccounts.end();
}
// make void because you can get size num_accts accounts.size()
// rename new_acct function to new_deposit because it doesn't necessarily add new account
// use ostream instead of ofstream (able to write to cout or file)
void new_deposit(ACCOUNT_LIST& bankaccounts, ostream& outfile)
{
int requested_account;
double new_deposit_amount = 0.00;
cout << endl << "Enter the account number: "; //prompt for account number
cin >> requested_account;
cout << endl << "Enter the deposit amount: "; //prompt for deposit amount
cin >> new_deposit_amount;
// used to see if account already exists
BankAccount acct(requested_account, new_deposit_amount);
ACCOUNT_LIST::iterator ite = findacct(acct, bankaccounts);
if(ite == bankaccounts.end())
{
outfile << endl << "Transaction Requested: New Account" << endl;
outfile << "New Account number: " << acct.account_number << endl;
bankaccounts.push_back(acct);
} else {
outfile << endl << "Transaction Requested: Existing Account" << endl;
// account_number of acct and *ite are same here so can use either
outfile << "Account number " << acct.account_number << endl;
// add the deposit to the existing account, use iterator
// instead of acct because we want to modify the original account
ite->balance += new_deposit_amount;
// do below so the last statement showing balance will
// reflect the original balance + deposit
acct = *ite; // dereference iterator and assign to acct variable
// (current balance in *ite will replace what
// was assigned initially as creation of acct variable)
}
outfile << "New Balance: $" << acct.balance << endl;
}