Here is the instructions from a book of mine:
Write a program that calculates the average number of days a company’s employees are absent.
The program should have the following functions:
• A function called by main that asks the user for the number of employees in the company. This value should be returned as an int. (The function accepts no arguments.)
• A function called by main that accepts one argument: the number of employees in the company. The function should ask the user to enter the number of days each employee missed during the past year. The total of these days should be returned as an int.
• A function called by main that takes two arguments: the number of employees in the company and the total number of days absent for all employees during the year. The function should return, as a double, the average number of days absent. (This function does not perform screen output and does not ask the user for input.)
Input Validation: Do not accept a number less than 1 for the number of employees. Do not accept a negative number for the days any employee missed.
This is shown as the answer:
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
|
#include "pch.h"
#include <iostream>
using namespace std;
//Define the function ret_no_emp();
int ret_no_emp()
{
//Declare the required variables.
int no_emp;
//Prompt the user to enter the input.
cout << "Enter the total number of employees in the company: " << endl;
cin >> no_emp;
//If the input entered was invalid, then
//run the loop to get the correct input.
while (no_emp < 1)
{
//Prompt the user to enter the valid input.
cout << "Number of employees should be at least 1";
cout << "Enter the total number of employees again: ";
cin >> no_emp;
}
//Return the total number of employees.
return no_emp;
//Declare the required variables.
int total = 0, leaves;
//Run the loop to get the
//leaves for each employee.
for (int i = 1; i <= no_emp; i++)
{
//Prompt the user to enter the
//leaves for the current employee.
cout << "Enter the total number of leaves in the past year for employee " << i << ": ";
cin >> leaves;
//If the input entered by the user was invalid
//then, run the loop to get the valid input.
while (leaves < 0)
{
//Prompt the user to enter the valid input.
cout << "The total number of leaves can not be a negative number" << endl;
cout << "Enter the total number of leaves again: " << endl;
cin >> leaves;
}
//Add the leaves of the current employee
//to the total number of leaves.
total = total + leaves;
}
//Return the total number of leaves.
return total;
}
//Define the function ret_avg_leaves().
double ret_avg_leaves(int no_emp, int total)
{
//Calculate and return the average
//number of leaves per employee.
return total / (double)no_emp;
}
int main()
{
//Display the average leaves taken by an employee.
cout << "Average leaves taken by an employee are: " << avg_leaves << endl;
system("pause");
return 0;
}
| |
But in int main(), VS says avg_leaves is undefined. Did the book mean to type something different? or is it a problem on my end?