You are using a version without Ads of this website. Please, consider donating:

### Functions and "Sum of"

I am new to programming and I'm currently in a c++ class. The program I am trying to write will:
1) Ask the user to input number of employees
2) Ask the user to input number of days missed for each employee
3) Calculate the average days missed by dividing (days missed) / (number of employees)

For this assignment we are starting to learn about functions. So the professor wants us to use a separate function for each step. I have attempted this program, but when I run it an put in numbers to test I keep getting 1.79149e-307. Regardless of what numbers I use. Listed below is the program I created. If you see where the mistake was made please let me know.

I have tried testing many things but I think the problem starts where it tries to total up the number of days missed in Line 32. For some reason instead of giving me the total. It gives me the last number I inputted. For example if I says 5 employees, and then say employee 5 missed 4 days. The program displays that the total days missed is 4.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include using namespace std; //Function Prototype int getNumEmp (); int getTotDayMiss (int); double getAvgDays (int, int); int main () { //Global Variable Declaration int NumEmp, TotDayMiss, N; double getAvgDays; //Function Call NumEmp = getNumEmp(); int total=0; //Loop for (int i=1; i<=NumEmp; i++) { total = 0; total += getTotDayMiss(i); } cout <<"Average days missed by employees: " << getAvgDays; return 0; } //Function 1 to get number of employees from user int getNumEmp () { int NumEmp; cout << "How many employees do you have? "; cin >> NumEmp; return NumEmp; } //Function 2 int getTotDayMiss (int NumEmp) { int DaysMissed; cout << "Enter number of day(s) missed for Employee " << NumEmp << ": "; cin >> DaysMissed; return DaysMissed; } //Function 3 double getAvgDays (int DaysMissed, int NumEmp) { double AvgDays; AvgDays = DaysMissed / NumEmp; return AvgDays; } ``````
try removing line 21
In this code snip

 ``1234567`` `````` for (int i=1; i<=NumEmp; i++) { total = 0; total += getTotDayMiss(i); } cout <<"Average days missed by employees: " << getAvgDays;``````

statement

total = 0;

shall be removed. And in this statement

cout <<"Average days missed by employees: " << getAvgDays;

the function name shall be replaced with the function call.
Last edited on
Topic archived. No new replies allowed.