function called by main

Hi, I'm working on this program and I wanted to know if I was going in the right direction or not. How am I doing? is there something I should do differently?

Here's a brief description of what I'm working on:

Write a program that calculates the average number of days a Joe's employees have been absent from work because of mining accidents. The program should have the following functions:

A function getEmployees, called by main that asks the user for the number of employees in the company. This value should be returned as an integer. (The function accepts no arguments.)

A function getDays, 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 integer.

A function computeAverage, 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.


My code so far. I know I'm missed things and I need help. Any tips please?
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 <iostream>
using namespace std;

int getEmployees();
int getDays();
double computeAverage();

int main()
{
	int numOfEmployees = 0;
	bool validInput = false;

	do
	{
		cout << "Please enter the number of employees in the company: ";
		cin >> numOfEmployees;

		validInput = (numOfEmployees >= 1);

		if (!validInput)
		{
			cout << "\nPlease enter a valid number!\n";
		}

	} while (!validInput);

	return numOfEmployees;
    
} 

int getEmployees(int numOfEmployees)
{
	cout << "The nuber of employess in the company is: " << numOfEmployees << endl;

	return numOfEmployees;

}

int getDays()
{
	int daysMissed = 0;
	bool validInput = false;

	do
	{
		cout << "Please enter the number of days each employee missed: ";
		cin >> daysMissed;

		validInput = (daysMissed >= 0);

		if (!validInput)
		{
			cout << "\nPlease no negative numbers!\n";
		}

	} while (!validInput);

	return daysMissed;

}

double computeAverage(int numOfEmployess, int daysMissed)
{
	return numOfEmployess * daysMissed;

}
Last edited on
Look at this part of your instructions.
A function getEmployees, called by main that asks the user for the number of employees in the company. This value should be returned as an integer. (The function accepts no arguments.)

Then look at this function implementation:
int getEmployees(int numOfEmployees)
You have a parameter when the instructions state no parameters.

Next:
A function getDays, 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 integer.
int getDays()
Note the lack of any arguments.

Lastly be careful here:
return numOfEmployess * daysMissed;
Both of those variables are integers so integer math is being used, you should probably cast at least one of those variables to a double to do the calculation. Plus you should be returning the average, how do you compute the average? Hint: you don't use multiplication.
I removed the parameter from the first function now. But is there anything in particular that I should write inside the function? I'm still new to programming and I'm confused. For the second one should I write "numOfEmployees" as a parameter? and for the last one ive changed it to division.
But is there anything in particular that I should write inside the function?

You need get the number of employees.
For the second one should I write "numOfEmployees" as a parameter?

What do your instructions tell you ?

and for the last one ive changed it to division.

And what else did you do? Just changing the operator will not solve the problem caused by using integer math, remember integers don't have fractions 1 / 2 will yield zero.

Please post your modified code and please refrain from posting in multiple forums at the same time.

This is what I've done. After input validation in my getDays function should I use what kind of loop to get the numbers of employees?
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
67
68
69
70
#include <iostream>
using namespace std;

int getEmployees();
int getDays();
double computeAverage();

int main()
{
	int numOfEmployees = getEmployees();
	bool validInput = false;
	int daysMissed = getDays(numOfEmployees);
	
	return 0;
    
} 

int getEmployees()
{
	int numOfEmployees = 0;
	bool validInput = false;

	do
	{
		cout << "Please enter the number of employees in the company: ";
		cin >> numOfEmployees;

		validInput = (numOfEmployees >= 1);

		if (!validInput)
		{
			cout << "\nPlease enter a valid number!\n";
		}

	} while (!validInput);

	return numOfEmployees;
    
}

int getDays(int numOfEmployees)
{
	int daysMissed = 0;
	bool validInput = false;

	do
	{
		cout << "Please enter the number of days each employee missed: ";
		cin >> daysMissed;

		validInput = (daysMissed >= 0);

		if (!validInput)
		{
			cout << "\nPlease no negative numbers!\n";
		}

	} while (!validInput);



	return daysMissed;

}

double computeAverage(double numOfEmployess, int daysMissed)
{
	return numOfEmployess / daysMissed;

}
Instead of the following:
1
2
3
4
5
double computeAverage(double numOfEmployess, int daysMissed)
{
	return numOfEmployess / daysMissed;

}

I myself would make the cast more apparent to the casual observer:

1
2
3
4
5
double computeAverage(int numOfEmployess, int daysMissed)
{
	return numOfEmployess / static_cast<double>(daysMissed);

}


Thanks for your help :)
Topic archived. No new replies allowed.