Link to home
Start Free TrialLog in
Avatar of Helix
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?
Avatar of Axter
Axter
Flag of United States of America image

Hi Helix,
You can pass as a pointer.

int getTotal(BankAccount *bankarray, int number);

David Maisonave :-)
Cheers!
You call above function with the following syntax:
getTotal(bankarray, 4);

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

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(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?
When you call the method, you need to pass the variable.

 BankAccount.getTotal(bankarray, 4);
correction:
bankarray[0].getTotal(bankarray, 4);
Avatar of Helix

ASKER

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'
Avatar of Helix

ASKER

Tried with the previous correction but no luck :(
Avatar of Helix

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(BankAccount *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::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;
}
>>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);
Avatar of Helix

ASKER

Still only returns 0 every time though.
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();
             
  }

     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(BankAccount *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.
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Helix

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.