Can someone help with my code?

Hi there! C++ newbie here. I was working on a problem, but ran into some problems. We have to submit our assignments through this thing called hypergrade (not sure if others use it) and it compiles everything for you. Anyway, when I compile it myself, it comes out correct, but hypergrade says I'm missing some lines. e-mailed our professor and he said:

"totalMissed += days; // Accumulate the number of days missed
to make sure you reset the days back to zero for every iteration of the loop.
Also, your final function should just be the return line for finding the average, then have the cout lines in the body of the main() function"

What does he mean? I'll attach the code. Thanks in advanced! Sorry for the length of this post :(

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
75
#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes:
int getNumEmps(void); // enter the # of employees
int getDaysMissed(int); // enter the days missed by each employee,
float avgDaysMissed(int, int); // calculate and return avg days missed

int main()
{
int employees, // record the # of company employees.
daysAbsent; // record the total days absent of all employees.
float daysPerEmp; // record the avg days absent per employee.

employees = getNumEmps();
daysAbsent = getDaysMissed(employees);
daysPerEmp = avgDaysMissed(employees, daysAbsent);

cout << "\nThe average number of days missed per ";
cout << "employee is " << daysPerEmp << "." << endl;

return 0;

}

int getNumEmps()
{
int emps;

// Obtain number of employees.
cout << "How many employees does the company have? ";
cin >> emps;

// Validate the input.
while (emps < 1)
{
cout << "\nNumber of employees must be one or greater.";
cin >> emps;
cout << "\nHow many employees does the company have? ";
}
return emps;

}

int getDaysMissed(int numEmps)
{
int days, // record the days missed by a given employee
totalMissed = 0; // Accumulates total days missed

for (int emp = 1; emp <= numEmps; emp++)
{
// obtain # of days missed
cout << "\nDays missed by Employee #" << setw(1) << emp <<"?";
cin >> days;
// validate input
while (days < 0)
{
cout << "\nDays missed must be zero or greater. ";
cout << "\nDays missed by Employee #" << setw(1) << emp <<"?";
cin >> days;
}
totalMissed += days; // Accumulate the number of days missed.
}

return totalMissed;
}

float avgDaysMissed(int emps, int days)
{

return static_cast<float>(days) / emps;

}
First off, you are unneccessarily using a float. An average usually gives you a decimal answer. A float will assume you want to get close to the number... but if you only get close the number, and again, and again, you get a miscalculated number. I would advise making your avgDaysMissed function an int, which will give you a more accurate answer.

"totalMissed += days; // Accumulate the number of days missed
to make sure you reset the days back to zero for every iteration of the loop.


So, for starters, your code:
totalMissed = 0; // Accumulates total days missed
is not defined as anything. I'm assuming an int? Secondly, he's asking you to set totalMissed back to zero every time you get a new employee(hint it's at the beginning of one of your for loops)

Also, your final function should just be the return line for finding the average,

I don't know your instructor whatsoever, but I imagine he means to make something like this happen:
1
2
3
4
5
int averageDaysMissed(int emps, int days)
{
      int average = days/emps;
      return average;
}


then have the cout lines in the body of the main() function"

I imagine you had cout statements in your last function before you posted this?

Good luck!
Last edited on
In response to myself, I noticed the last part(the two last things I quoted) were both about the cout statements. If your function is working properly, you shouldn't change it.
Thanks for replying! The calculated numbers are correct. So I'm still confused lol. The code I submitted here is the same I used. The site we have to upload it says I'm missing a few lines. I tried moving everything but the last line into the int main () but it just messes it up more. I tried doing

for (totalMissed=0; int emp = 1; emp <= numworker; emp++)

but then I get an error. Is that what you meant? sorry, c++ is so difficult for me lol.
Topic archived. No new replies allowed.