Code won't let me divide

Hello guys, I'm currently in the process of my final coding exam and i encounter a problem. My code won't divide nor display result at the end. I tried to sum,multiply and those work, but it doesn't divide. I know it's some kind of error on my part but I need help finding it. I need the average of days absent with the total number of employees and the total number of days absent.
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
#include <iostream>
#include<iomanip>

using namespace std;

int main()
{

   int numEmployee;
   int numAbsent;
   int* absent;
   int days;
   float percent;
   int daysA;




   cout << "Employees in the company: ";
   cin >> numEmployee;
   cout << "Employees absent in the company: ";
   cin >> numAbsent;

   absent = new int[numAbsent];

   for ( int x = 0; x < numAbsent; x++ )
   {
     cout << "Absence: ";
     cin >> absent[x];


}


for(int x = 0; x < numAbsent; x++ ){
 daysA = days += absent[x];
}
cout << "Employees in the company: " << numEmployee << endl;
cout << "Total number of days absent for all employees during the year: " << days << endl;
percent = (numEmployee / daysA);  //<------ This part right here!
cout << percent;                  //When I run it, the console displays a 0.
cin.get(); }              
Because they are both ints. Change them to float or double and you'll be set :)
I think your problem may be in line 24 of code presented, what is "new int". Possibly also in line 11 too. "int*"
about the "new int" and the "int*" I saw a video of arrays and since I need to store a number of employee's and each of their absence, I saw it suitable. I don't know if there's a better way. I changed the int to float and it divides but it gives me a huge number that's not close to the result I'm looking for.
Just a way of making an array of variable size at runtime. A better way would just be to use a vector.

You changed both numEmployee and daysA to float/double?
daysA = days += absent[x];

days is not initialized to or assigned any meaningful value. It could be anything and because of that, daysA may be anything. Pay attention to what your compiler is telling you, and if it isn't telling you anything turn up your warning level.

percent = numEmployee / static_cast<float>(daysA);
Well, I don't understand why you have both the variables days and daysA if they equal each other (if daysA can be replaced by days and you save yourself from making another copy of a variable then do it).

The percentage is off because you are dividing the number of employees by the days absent instead of the other way around. Try dividing daysA/numEmployee and it should work better. (this is because the percent is talking about days absent per employee and not the other way around which would be nonsensical)

daysA and numEmployee are just fine as int because you can't have half of an employee, and in this case I find it unlikely that someone will enter a fraction of a day absent. (This assumption may be wrong if you are talking about part-time employees or say you are including people who only showed up for a half-day of work or went home early etc, in which case you would want to assign them as float).
Last edited on
Ok, I finally managed to get a result. Here's what I had to do.

percent = (static_cast<float>(days)/365)*100;

Thanks everyone!
Last edited on
Topic archived. No new replies allowed.