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