### Problem with getting an average from a function

Hey guys, bit of a problem here. We're just getting into functions in my C++ class, and I'm having trouble getting the average to go to main to be output. It always outputs the average as 0, but everything else goes through just fine. I'd really appreciate some tips/advice on what to do and to possibly explain what I'm doing wrong. It's not due till next Tuesday, so there's plenty of time.

I suppose the problems would probably be on lines: 8, 21, 25, and 76-81 if that helps at all.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081`` ``````#include #include using namespace std; int employees(); int days(int); double average(int, int); int main() { char repeat; int totalEmployees; int totalDaysMissed; double finalAverageMissed; do { totalEmployees = employees(); totalDaysMissed = days(totalEmployees); finalAverageMissed = average(totalEmployees, totalDaysMissed); cout << "\nThere are " << totalEmployees << " employees in the company." << endl; cout << "In total, they missed " << totalDaysMissed << " days." << endl; cout << "The average of the total days missed is: " << finalAverageMissed << endl; cout << "\nWould you like to run the program again? (Y/N)" << endl; cin >> repeat; cout << endl; }while(repeat == 'y' || repeat == 'Y'); return 0; } int employees() { int numEmployees; cout << "Please enter the number of employees in the company." << endl; cin >> numEmployees; while(numEmployees < 1) { cout << "Please enter a number greater than 1 for the number of employees." << endl; cin >> numEmployees; } return numEmployees; } int days(int numEmployees) { int daysMissed; int totalMissed = 0; cout << "\nPlease enter the total days missed for each employee." << endl; for (int i = 0; i < numEmployees; i++) { cout << "Employee #" << (i + 1) << ": "; cin >> daysMissed; while(days < 0) { cout << "Please enter a positive number for the days missed." << endl; cin >> numEmployees; } totalMissed += daysMissed; } return totalMissed; } double average(int totalMissed, int numEmployees) { double averageMissed = (totalMissed / numEmployees); return averageMissed; }``````
At line 78 you're doing integer division, so unless totalMissed is > num_Employees you're going to be assigning 0 to averageMissed. Cast totalMissed and numEmployees to `double` BEFORE dividing.
Ah, I could see where that would've caused an issue. Didn't pop into my mind to do that though. Also had to switch

`double average(double totalMissed, double numEmployees)`

in line 76 to

`double average(double numEmployees, double totalMissed)`

or else it would divide the number of employees by the missed days instead of the way it's supposed to. I don't really see how that would cause a problem though, but as soon as I saw it I figured it out. It's working just fine now, just a little bit confused as to why the arguments have to be in that order.
The arguments have to be in that order, because they are positional and that is the order you specify them in line 21:
 `` `` ``finalAverageMissed = average(totalEmployees, totalDaysMissed);``
Makes sense, won't make that mistake in the future. Thank you for the help!
Topic archived. No new replies allowed.