• Status: Solved
• Priority: Medium
• Security: Public
• Views: 252

# Passing array into functions

Hey everyone, ive wrote a program to simulate bank balances etc, i've managed to write some code into main() to add up all the balances etc. :

int main()
{
int totalBalance = 0;

BankAccount bankarray[4];
bankarray[0] = BankAccount (1000,100.10);
bankarray[1] = BankAccount (2000,200.20);
bankarray[2] = BankAccount (3000,300.30);
bankarray[3] = BankAccount (4000,400.40);

for (int i = 0; i < 4; i++)
{
totalBalance +=  bankarray[i].getBalance();

}

cout << "Total of balances in array : \$" <<  totalBalance << endl;

return 0;
}

This works abolsiutly fine.

Im just wondering how i can pass a bankarray to a function, ive tried defining  it as :

int getTotal(bankarray[], int number);

for the bank array, size of array etc. but its not working. Ive also tried a normal array but that'll obviously wont work. Can anyone help please?
0
Helix
• 9
• 6
1 Solution

Commented:
Hi Helix,
You can pass as a pointer.

int getTotal(BankAccount *bankarray, int number);

David Maisonave :-)
Cheers!
0

Commented:
You call above function with the following syntax:
getTotal(bankarray, 4);

Or
getTotal(bankarray, sizeof(bankarray)/sizeof(bankarray[0]));
0

Author Commented:
aha, progressed a little.

int main()
{
int totalBalance = 0;

BankAccount bankarray[4];
bankarray[0] = BankAccount (1000,100.10);
bankarray[1] = BankAccount (2000,200.20);
bankarray[2] = BankAccount (3000,300.30);
bankarray[3] = BankAccount (4000,400.40);

BankAccount::getTotal(BankAccount *bankarray, 4);

return 0;
}

int BankAccount::getTotal(BankAccount *bankarray,int number)
{
int totalBalance;
for (int i = 0; i < 4; i++)
{
totalBalance +=  bankarray[i].getBalance();

}

}

But its not quite working. Allllsooo.. in the getTotal function, is there an easy why to pass int totalBalance into main without having to define it in the class?
0

Commented:
When you call the method, you need to pass the variable.

BankAccount.getTotal(bankarray, 4);
0

Commented:
correction:
bankarray[0].getTotal(bankarray, 4);
0

Author Commented:
int BankAccount::getTotal(BankAccount *bankarray,int number)
{
int totalBalance;
for (int i = 0; i < number; i++)
{
totalBalance +=  bankarray[i].getBalance();

}
}

int main()
{
int totalBalance = 0;

BankAccount bankarray[4];
bankarray[0] = BankAccount (1000,100.10);
bankarray[1] = BankAccount (2000,200.20);
bankarray[2] = BankAccount (3000,300.30);
bankarray[3] = BankAccount (4000,400.40);

BankAccount::getTotal(bankarray, 4);
//cout << "Total of balances in array : \$" <<  totalBalance << endl;

return 0;
}

That gives me a error :

error C2352: 'BankAccount::getTotal' : illegal call of non-static member function
C:\Documents and Settings\David\My Documents\C++\Exercise 7\5.cpp(17) : see declaration of 'getTotal'
0

Author Commented:
Tried with the previous correction but no luck :(
0

Author Commented:
Whole code:

#include <iostream>
using namespace std;

class BankAccount{

public:
BankAccount( int num, double amount);
BankAccount();

void deposit(double amount);
void withdraw( double amount);

int getAccNum() const;
double getBalance() const;

void printDetails()const;
int getTotal(BankAccount *bankarray, int number);
protected:
double balance;
int accNum;
};

int BankAccount::getTotal(BankAccount *bankarray,int number)
{
double total;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();

}

}
BankAccount::BankAccount (int num, double amount)
{
balance = amount;
accNum = num;
}

BankAccount::BankAccount()
{
balance = 20000;
accNum = 100;
}

void BankAccount::deposit (double amount)
{
balance+= amount;
}

void BankAccount::withdraw (double amount)
{
balance -=amount;
}

double BankAccount::getBalance() const
{
return balance;
}

int BankAccount::getAccNum() const
{
return accNum;
}

void BankAccount::printDetails() const
{
cout<<" Account number "<< accNum <<" has a balance of " << balance << endl;
}

class ChequeAccount: public BankAccount{
public:
ChequeAccount(int num, double amount, double charge);
double GetOverdraft();
int getCharge();
void withdraw(double amount);
private:
double overdraft;
};

ChequeAccount::ChequeAccount(int num, double amount, double charge)
{
balance = amount;
accNum = num;
overdraft = charge;

withdraw(charge);
}

int ChequeAccount::getCharge()
{
return overdraft;
}

double ChequeAccount::GetOverdraft()
{
return overdraft;
}

void ChequeAccount::withdraw(double amount)
{
if (balance < amount )
balance -= amount + overdraft;
else
balance -=amount;
}

int main()
{

BankAccount *bankarray[4];

bankarray[0] = &BankAccount (1000,100.10);
bankarray[1] = &BankAccount (2000,200.20);
bankarray[2] = &BankAccount (3000,300.30);
bankarray[3] = &BankAccount (4000,400.40);

cout << bankarray[0]->getTotal(*bankarray, 4);

return 0;
}
0

Commented:
>>bankarray[0]->getTotal(*bankarray, 4);

The asteric is not suppose to be there, and neither is the ->

It should be exactly the way I posted it previously.

bankarray[0].getTotal(bankarray, 4);
You could also do
bankarray->getTotal(bankarray, 4);
0

Author Commented:
Still only returns 0 every time though.
0

Commented:
Please post the new code.

Also there is a problem with the function:
int BankAccount::getTotal(BankAccount *bankarray,int number)
{
double total;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();

}

}

The results of the above function is considered undefined, since total is never initialized to a value.
Also, total should be the same type as the return type.

double BankAccount::getTotal(BankAccount *bankarray,int number)
{
double total = 0;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();

}

}

If you still get a 0 then you should check the values for your array.
0

Commented:
More errors.

BankAccount *bankarray[4];// *** this should not have an asteric

//The following will not work for a concrete type
bankarray[0] = &BankAccount (1000,100.10);
bankarray[1] = &BankAccount (2000,200.20);
bankarray[2] = &BankAccount (3000,300.30);
bankarray[3] = &BankAccount (4000,400.40);

Do you want to use an array of pointers, or a fix size concrete array?
0

Author Commented:
I just changed the return type to double, initialised total to 0 and now it works fine, thank you :)
0

Commented:
Why the B grade?
0

Commented:
This is a 30 point, and I know I gave you way more then a A grades worth for a 30 point question.
0

## Featured Post

• 9
• 6
Tackle projects and never again get stuck behind a technical roadblock.