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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
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

1
2
3
4
5
6
7
	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.