Helix
asked on
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?
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?
You call above function with the following syntax:
getTotal(bankarray, 4);
Or
getTotal(bankarray, sizeof(bankarray)/sizeof(b ankarray[0 ]));
getTotal(bankarray, 4);
Or
getTotal(bankarray, sizeof(bankarray)/sizeof(b
ASKER
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(Bank Account *bankarray, 4);
return 0;
}
int BankAccount::getTotal(Bank Account *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?
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(Bank
return 0;
}
int BankAccount::getTotal(Bank
{
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?
When you call the method, you need to pass the variable.
BankAccount.getTotal(banka rray, 4);
BankAccount.getTotal(banka
correction:
bankarray[0].getTotal(bank array, 4);
bankarray[0].getTotal(bank
ASKER
int BankAccount::getTotal(Bank Account *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(bank array, 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'
{
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(bank
//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'
ASKER
Tried with the previous correction but no luck :(
ASKER
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(Bank Account *bankarray,int number)
{
double total;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();
}
return total;
}
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::ChequeAccou nt(int num, double amount, double charge)
{
balance = amount;
accNum = num;
overdraft = charge;
withdraw(charge);
}
int ChequeAccount::getCharge()
{
return overdraft;
}
double ChequeAccount::GetOverdraf t()
{
return overdraft;
}
void ChequeAccount::withdraw(do uble 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(*ba nkarray, 4);
return 0;
}
#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(Bank
{
double total;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();
}
return total;
}
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(
{
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::ChequeAccou
{
balance = amount;
accNum = num;
overdraft = charge;
withdraw(charge);
}
int ChequeAccount::getCharge()
{
return overdraft;
}
double ChequeAccount::GetOverdraf
{
return overdraft;
}
void ChequeAccount::withdraw(do
{
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(*ba
return 0;
}
>>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(bank array, 4);
You could also do
bankarray->getTotal(bankar ray, 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(bank
You could also do
bankarray->getTotal(bankar
ASKER
Still only returns 0 every time though.
Please post the new code.
Also there is a problem with the function:
int BankAccount::getTotal(Bank Account *bankarray,int number)
{
double total;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();
}
return total;
}
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(Bank Account *bankarray,int number)
{
double total = 0;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();
}
return total;
}
If you still get a 0 then you should check the values for your array.
Also there is a problem with the function:
int BankAccount::getTotal(Bank
{
double total;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();
}
return total;
}
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(Bank
{
double total = 0;
for (int i = 0; i < number; i++)
{
total += bankarray[i].getBalance();
}
return total;
}
If you still get a 0 then you should check the values for your array.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I just changed the return type to double, initialised total to 0 and now it works fine, thank you :)
Why the B grade?
This is a 30 point, and I know I gave you way more then a A grades worth for a 30 point question.
You can pass as a pointer.
int getTotal(BankAccount *bankarray, int number);
David Maisonave :-)
Cheers!