order of code not letting cout show?

I'm writing a program that shows the average leaves taken by an employee. I'm trying to figure out what the problem is with the order. I've been told that there's a return that's blocking the program from displaying the first part, but I can't find what one is doing it.

When I run it, all that is showing is "Average leaves taken by an employee are: 0"

All in all, I just want this program to allow the user to leave input before displaying 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
#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.";
		cout << "Enter the total number of employees again: ";
		cin >> numEmployee;
	}

	for (int i = 1; i <= numEmployee; i++)
	{
		int total = 0;
		int leaves;

		cout << "Enter the 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;
	}

	return numEmployee;
}

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

int main()
{
	cout << "Average leaves taken by an employee are: " << averaLeaves << endl;

	system("pause");
	return 0;
}
Last edited on
Where have you declared "averaLeaves"? You have declared averaLeavesRetu. And you haven't called the other function..

You're getting a 0 because in line 5 you have declared averaLeaves but the compiler sees no redefinition of it.



@Nwb

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;
}
26, 45, 51, 52 are now invalid.

It helps if you tell us the actual error messages.

At 26 and 45, you're trying to use a variable that doesn't exist in that scope.

At 51, you're attempting to call a function that you haven't defined.

Last edited on
Isn't this a duplicate of:

http://www.cplusplus.com/forum/beginner/246148/

Please don't have multiple threads for the same question. It wastes everyone's time, including your own.
Last edited on
Topic archived. No new replies allowed.