Need help with a c++ project

Here is what i am told to do:

Write a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt". 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 two arguments:
 The name of the file stream object as a reference parameter of type ofstream, and the number of employees in the company.
 The function should ask the user to enter as integers for each employee: the employee number (ID) and the number of days that employee missed during the past year.
 Each employee number (ID) and the number of days missed should be written to the report file in this function.
 The total of these days should be returned as an int. Assume the employee number (ID) is 4 digits or fewer, but don't validate it.
• 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 or file 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.
• Be sure to print appropriate error messages for these items if the input is invalid.




I have written my code (Below). I have not included my in or out file yet since i am trying to work on the logic first.

HERE IS MY QUESTION:
I am not able to loop (employeeId)
if you can help, please let me know as its due tonight.

//Razafindragolo, Tanguy X
//Nov 13 (2 days ago)

#include<iostream>
#include<iomanip>
using namespace std;

int employee_Number();
int employeeId(int);
int daysNumber(int);
double average_abs(int, int);
int main()
{
int employeeNum_data = 0;
int employeeId_data = 0;
int daysNumber_data = 0;
double absAverage_data = 0.0;
employeeNum_data = employee_Number();
employeeId_data = employeeId(employeeId_data);
daysNumber_data = daysNumber(employeeNum_data);
absAverage_data = average_abs(employeeNum_data, daysNumber_data);
cout << "The average employee absent days last year: " << endl << absAverage_data;
cout << endl;
system("pause");
return 0;
}
int employee_Number()
{
int employeeNumber_1;

cout << "Please enter the number of the employees in the company: " << endl;
cin >> employeeNumber_1;

while (employeeNumber_1 < 1)
{
cout << "The number of employee must be greater than 0.";
cout << "Please re-enter the number of employee.";
cin >> employeeNumber_1;
}
return employeeNumber_1;
}
int employeeId(int d)
{
int employeeId_1;
//int employeeNumber_1 = d;

while (employeeId_1 != 0)
{
cout << "Please enter an employee ID:";
cin >> employeeId_1;

if ( employeeId_1 < 1)
{
cout << "The number of employee ID should be greater than 0" << endl;
cout << "Please re-enter the number of employee.";
cin >> employeeId_1;
}

}

return employeeId_1;
}
int daysNumber(int e)
{
int employeeNumber_1 = e;
int daysNumber_1;
int total_days = 0;

for (int i = 0; i < employeeNumber_1; i++)
{
cout << "Enter number of Days " << i + 1 << " employee missed last year" << endl;
cin >> daysNumber_1;
total_days += daysNumber_1;
}

while (employeeNumber_1 < 0)
{
cout << "The number of days must not be negative.";
cout << "Please re-enter the number of days absent:";
cin >> daysNumber_1;
}
return total_days;
}
double average_abs(int a, int b)
{
int employeeNumber_2 = a;
int daysNumber_2 = b;
double absAverage_1;

absAverage_1 = (employeeNumber_2 * 365) / daysNumber_2;
return absAverage_1;

}
In your employeeID function your variable employeeID_1 is tested before being initialized. What if you did something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int employeeId(int d)
{
	int employeeId_1 = -1;

	do {
		cout << "Please enter an employee ID:";
		cin >> employeeId_1;

		if (employeeId_1 < 1)
		{
			cout << "The number of employee ID should be greater than 0" << endl;
		}

	} while (employeeId_1 < 0);

	return employeeId_1;
}
Yes, that makes more sense. I see what you're saying compared to what i wrote. Appreciate it.
While i was waiting help, i played around my code and ended up with this which solved my problem but now my calculation is not right:

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
71
72
73
74
#include<iostream>
#include<iomanip>
using namespace std;

int employee_Number();
int daysNumber(int);
double average_abs(int, int);
int main()
{
	int employeeNum_data = 0;
	int employeeId_data = 0; 
	int daysNumber_data = 0;
	double absAverage_data = 0.0;
	employeeNum_data = employee_Number();
	daysNumber_data = daysNumber(employeeNum_data);
	absAverage_data = average_abs(employeeNum_data, daysNumber_data);
	cout << "The average employee absent days last year: " << endl << absAverage_data;
	cout << endl;
	system("pause");
	return 0;
}
int employee_Number()
{
	int employeeNumber_1;

	cout << "Please enter the number of the employees in the company: ";
	cin >> employeeNumber_1;

	while (employeeNumber_1 < 1)
	{
		cout << "The number of employee must be greater than 0.";
		cout << "Please re-enter the number of employee.";
		cin >> employeeNumber_1;
	}
	return employeeNumber_1;
}

int daysNumber(int e)
{
	int employeeNumber_1 = e;
	int daysNumber_1;
	int total_days = 0;

	for (int i = 0; i < employeeNumber_1; i++)
	{
		int employeeId;

		cout << "Please enter employee ID: ";
		cin >> employeeId;
		cout << "Enter number of Days " << employeeId << "  missed last year: ";
		cin >> daysNumber_1;
		total_days += daysNumber_1;

		if (daysNumber_1 < 0)
		{
			cout << "The number of days must not be negative." << endl;
			cout << "Please re-enter the number of days absent: ";
			cin >> daysNumber_1;
		}
	}

	
	return total_days;
}
double average_abs(int a, int b)
{
	int employeeNumber_1 = a;
	int total_days = b;
	double absAverage_1;

	absAverage_1 = total_days /employeeNumber_1;
	return absAverage_1;

}


Last edited on
What calculation is not right? Rough line number?
average_abs
line 66-74
average_abs

I haven’t tested your code, but it seems your function performs an integer division.
If you divide two integers, you obtain an integer. To get a double, at least one of the two operands must be a floating point number:
1
2
3
4
double average_abs(int a, int b)
{
    return static_cast<double>(a) / static_cast<double>(b) ;
}

Topic archived. No new replies allowed.