I am working on an exercise from my textbook, Staring Out With C++: Early Objects. (I am not asking for you to solve.) The exercise calls for calculating and displaying the average number of days a company's employees are absent with the use of three functions. One that asks for the number of employees. One that asks for the number of days missed by each employee, which you can use to find that total number of days missed. The third function calculates the average number of days missed per employee.
I am working on a Mac with Mac OS version 10.6.2 and am using CodeBlocks 8.02.
The program compiles without error but when I run it, it gives me an error after I enter in the first value. The error is "floating point exception." In researching floating point exceptions I found that it arises when your code is trying to something it is not allowed to do (like sqrt of a negative number). I cannot find where that error is in my code, however.
I would appreciate any help in finding the error in my code. Thanks.
// Functions defined
int getNumEmployees(int employees)
{
cout << "Please enter the number of employees: ";
cin >> employees;
return employees;
}
int getTotalDays(int totaldays, int employees)
{
int days; // Holds the number of days absent per employee
int count = 1; // Initinialize the employee counter at 1
while (count <= employees)
{
cout << "Please enter the number of absent days for ";
cout << "each employee: ";
cin >> days;
totaldays += days;
count++;
}
return totaldays;
}
You are dividing by zero in find average since none of your functions are actually modifying the variables you pass. I think you are intending to make all of the functions void, and pass the variables by reference. You could also not pass the variables and instead simple assign the value you are returning from the functions to the correct variable.
You're right. I changed the functions to void functions so they would pass the variables by reference and it worked with some other adjustments. Thanks for you help.