How to add a sum of the total amount of pay in an array?

Say, I have my calculateSalary() and it is able to print out each and every staff they have in the array. How can i implement a function to add all of the staff's salary together?
closed account (10oTURfi)
1
2
3
4
5
int total=0;
for(int a=0; my_array[a]; a++)
    total+=my_array[a];

cout << total << endl;
Last edited on
so in terms of adding all staff's salary, is this right?

1
2
3
4
int total = 0;
for(int i = 0; empArray[i]; i++)
     total += empArray[i].salary;
cout << total << endl;
Because i have an array storing my staff objects which consist of derived classes.
Each objects have their fields like name, address, calculateSalary().

In this case u suggest i do a for loop to run through the array. But my array is defined in my base class.
I created a displayAllSalary() in my base class and derive class and my array is being defined in my main.cpp
Can i define another array in my base and derive class? Of same name to use it again?
Any help available..?
Can you post more of the code?
do you have a class called employee? and each staff is an object. Or you just have all the staff's name and their salary in a two dimensional array?
I solved it already. I just need to code this in my main.cpp
1
2
3
4
int total = 0;
for(int i = 0; empArray[i]; i++)
     total += empArray[i]->salary;
cout << total << endl;

and it worked. thanks
Topic archived. No new replies allowed.