Why is this undefined?

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?
Last edited on
Your instructions say to define three functions in addition to main. You only have two, although one of them has a return statement in the middle that causes the second half to never execute; looks like the second half should be a separate function. It also says that you are to call those functions from main. So main should look something like this:

1
2
3
4
5
6
int main() {
    int num_emps = get_num_emps();
    int total = get_missed_days(num_emps);
    double avg = calc_avg(num_emps, total);
    cout << "Average days missed: " << avg << '\n';
}

Last edited on
> But in int main(), VS says avg_leaves is undefined.
¿where have you defined it? ¿where have you set it a value?
@tbp
@ne555

Got another function in here now. 26, 45, 51, 52 are now invalid.

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
#include "pch.h"
#include <iostream>
using namespace std;

int averaLeaves;

int employNumRetu()
{
	int numEmployee;
	cout << "Enter the total number of employees in the company: " << endl;
	cin >> numEmployee;

	while (numEmployee < 1)
	{
		cout << "Number of employees should be at least 1. \n";
		cout << "Enter the total number of employees again: ";
		cin >> numEmployee;
	}
	return numEmployee;
}

double totalLeavesRetu()
{
	int total;
	int leaves;
	for (int i = 1; i <= numEmployee; i++)
	{
		cout << "Total number of leaves in the past year for employee " << i << ": ";
		cin >> leaves;
	}

	while (leaves < 0)
	{
		cout << "The total number of leaves can not be a negative number." << endl;
		cout << "Enter the total number of leaves again: " << endl;
		cin >> leaves;
	}
	total = total + leaves;
	return total;
}


double averaLeavesRetu(int numEmployee, int total)
{
	averaLeaves = return total / (double)numEmployee;
	return averaLeaves;
}

int main()
{
	int numEmployee = numEmployeeRetu();
	int total = totalLeavesRetu(numEmployee);
	double averaLeaves = averaLeavesRetu(numEmployee, total);

	cout << "Average leaves taken by an employee are: " << averaLeaves << endl;

	system("pause");
	return 0;
}
line 51:
you define function int employNumRetu() but try to call numEmployeeRetu();

line 52:
you said that the function takes no parameter double totalLeavesRetu()
but try to pass one totalLeavesRetu(numEmployee);

line 45:
¿are you smashing the keyboard now? averaLeaves = /*return*/ total / (double)numEmployee;

line 26:
¿where have you defined numEmployee?
https://msdn.microsoft.com/en-us/library/b7kfh662.aspx
Hello quete5,

Line 26 like ne555 said you have the variable name "numEmployee", but where is it defined?

Line 45 is the same thing "averaLeaves" is used, but never defined.

Line 51 you call the function"numEmployeeRetu()", but where is it defined?

The function that you have int employNumRetu()
The call you make................numEmployeeRetu()
See a difference?

Line 52
The function that you have double totalLeavesRetu()
The call you make.........................totalLeavesRetu(numEmployee)
See a difference?

And in line 45 the return statement will leave the function after doing the calculation and before "averaLeaves" would receive a value. The function could just contain one line return total / (double)numEmployee;

Hope that helps,

Andy
Topic archived. No new replies allowed.