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.
#include <iostream>
#include <cmath>
usingnamespace 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.
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.