Function not storing data in array.

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


new_acct Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int new_acct(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile)
{
		int requested_account;
		int index;
		double new_deposit_amount = 0.00;

		cout << endl << "Enter the account number: ";           //prompt for account number
		cin >> requested_account;

		index = findacct(acctnum_array, num_accts, requested_account);
		if (index == -1)                                        //valid account
		{	
			num_accts++;        //number of accounts
			outfile << endl << "Transaction Requested: New Account" << endl;
			outfile << "Account Number: " << requested_account << endl;
			acctnum_array[num_accts] = requested_account;
			balance_array[num_accts] = new_deposit_amount;
			outfile << "New Account number: " << acctnum_array[num_accts] << endl;
			outfile << "New Balance: $" << balance_array[num_accts] << endl;

		}
		else                                                    //valid account
		{
			outfile << endl << "Transaction Requested: New Account" << endl;
			outfile << "Error: Account number " << requested_account << " already exist" << endl;
		}
	return num_accts;
}
Last edited on
OK i just found out it does store the data but in a different form something like this:

account number: -858999955555
account balance: -925556641112200000000000

why is doing that???
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
    bool operator==(const BankAccount& rhs) const
    { return (this->account_number == rhs.account_number); }
    // use not of ==
    bool operator!=(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();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

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

}



Topic archived. No new replies allowed.