Arrays in fuction not calculating division

So I have this code to calculate and display shareholders and their shares. It compiles fine and it all works except for the percentage of total shares function (computeSharePercentage) it just returns zeros as if its not doing the calculation at all and I can't figure out why, any help would be great!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120`` ``````#include #include #include #include #include using namespace std; void getInputs(string* holders, int* shares); void computeTotalShares(int* shares, int& totalshares); void computeSharePercentage(int* shares, int& totalshares, double* percentage); void getLargestShare(int* shares, double& high); void outputResults(string* holders, int* shares, int& totalshares, double* percentage, double& high); int main() { string holders[5]; int shares[5]; int totalshares=0; double percentage[5]; double high; getInputs(holders, shares); computeTotalShares(shares, totalshares); computeSharePercentage(shares, totalshares, percentage); getLargestShare(shares, high); outputResults(holders, shares, totalshares, percentage, high); } void getInputs(string* holders, int* shares) { int i; for(i=0;i<5;i++) { if(i==0) { cout << "Enter the first Shareholder's name: \n"; } else { cout << "Enter another Shareholder's name: \n"; } cin >> holders[i]; cout << "How many shares do they own?\n"; cin >> shares[i]; } } void computeTotalShares(int* shares, int& totalshares) { int t; for(t=0;t<5;t++) { totalshares = totalshares + shares[t]; } } void computeSharePercentage(int* shares, int& totalshares, double* percentage) { int p; for(p=0;p<5;p++) { percentage[p] = (shares[p]/totalshares)*100; } } void getLargestShare(int* shares, double& high) { int l; for(l=0;l<5;l++){ if (shares[l] > high) { high = shares[l]; } } } void outputResults(string* holders, int* shares, int& totalshares, double* percentage, double& high) { int o, h, f; cout << "Shareholder Share Amount % of Total Shares\n"; cout << fixed << setprecision(2) << endl; for(o=0;o<5;o++) { cout << left << setw(15) << holders[o] << left << setw(20) << shares[o] << percentage[o] << endl; } cout << fixed << setprecision(0); cout << "Total " << totalshares << endl; for(h=0;h<5;h++){ if (shares[h] > high) { high = shares[h]; } } for(f=0;f<5;f++){ if (high == shares[f]) { cout << "The Highest Shareholder is " << holders[f]; } } } ``````
That's because shares[p] and total shares are both integers. Dividing a smaller integer by a larger integer gives 0. You need to cast one of them into a double.
Thanks!
Topic archived. No new replies allowed.