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

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
Asked:
Helix
  • 9
  • 6
1 Solution
 
AxterCommented:
Hi Helix,
You can pass as a pointer.

int getTotal(BankAccount *bankarray, int number);

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

Or
getTotal(bankarray, sizeof(bankarray)/sizeof(bankarray[0]));
0
 
HelixAuthor 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

 BankAccount.getTotal(bankarray, 4);
0
 
AxterCommented:
correction:
bankarray[0].getTotal(bankarray, 4);
0
 
HelixAuthor 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
 
HelixAuthor Commented:
Tried with the previous correction but no luck :(
0
 
HelixAuthor 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();
              
  }

      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;
}
0
 
AxterCommented:
>>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
 
HelixAuthor Commented:
Still only returns 0 every time though.
0
 
AxterCommented:
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.
0
 
AxterCommented:
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
 
HelixAuthor Commented:
I just changed the return type to double, initialised total to 0 and now it works fine, thank you :)
0
 
AxterCommented:
Why the B grade?
0
 
AxterCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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