Pass values by reference

I built a program that finds the average amount of days missed per employee. I am now attempting to modify my program to pass values by reference rather than passing by value. I created 3 functions:
int numOfEmployees(); int numOfDays(int); double avgDays(int, int);

in the prototype I use the ampersand sign int numOfDays(int&);
in the actual function I use numOfDays(int& employees)

I am just not able to get the call to the function to work the program will crash when I modify it with the ampersand signs.

This is the original code I am trying to modify:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;

int numOfEmployees();              //Prototype for numOfEmployees
int numOfDays(int);                //Prototype for numOfDays
double avgDays(int, int);          //Prototype for avgDays

int main()
{ 
 int employNum;    //Sets memory for employNum
 int amountSick;   //Sets memory for amountSick
 double average;   //Sets memory for average            

 employNum = numOfEmployees();                   //Calls the numOfEmployees() function
 amountSick = numOfDays(employNum);              //Calls the numOfDays() function
 average = avgDays(employNum,amountSick);        //Calls the avgDays() 
 
 //Displays the average amount of days missed
 cout << endl;
 cout << "The average amount of days missed is " <<average<<endl;
 cout << endl;
 
 //End of main
 system ("pause");
 return 0;
}
//********************************Functions for program below**********************************************************\\

//numOfEmployees function asks the user for the number of employees. Accepts only int values greater than 1
	int numOfEmployees()
	{
         int employees;
		
         cout << "How many employees? ";
	 cin  >> employees;
	 cout << endl;
		
	//Employees must be greater than 0!
	while(employees < 1)
	{
	 cout << "The number of employees must be greater than 1. Please try      again." <<endl;
	 cout << "How many employees? ";
         cin  >> employees;
         cout << endl;
	}
		
		return employees; 
	}
	
	//NumOfDays function asks the user for the amount of days sick per employee
	int numOfDays(int employees)
	{   
	    int num = 1;
	    int totalDays = 0;
		
        //This loop will iterate as many times as there are employees
        for (employees; employees > 0; employees--)
         {  
            int days;
            
         	cout << "How many days has employee " <<num<< " been sick? ";
         	cin >> days;
         	
         	//The number of days can't be less than 0
         	while (days < 0)
         	{   
         	    cout << endl;
         		cout << "The number of days missed can not be a negative number. Please try again."<<endl;
         		cout << "How many days has employee " <<num<< " been sick? ";

         	    cin  >> days;
         	    cout << endl;
         	}
         	
         	 //Keeps a tally for the total amount of days missed
	         totalDays = totalDays + days;
         	 
         	 //Increments number every iteration for the output "How many days has employee " <<num<< " been sick?"
			 num ++;
         }
         
         return totalDays;
	}
	
	//avgDays function finds the average amount of days missed for an employee
	double avgDays(int employees, int totalDays)
	{
	  double avg;
	  
	  avg =	totalDays/employees;
	  
	  return avg;
	}
numOfDays() changes the value of its formal parameter, but the caller doesn't expect the value of the actual parameter to change, so you're breaking your contract (so to speak) if you change employees into a reference willy-nilly. You need to modify the function to avoid writes to employees, but if you're going to do that you may as well not bother and simply leave everything as it was, without references.
Topic archived. No new replies allowed.