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.
#include <iostream>
#include<iomanip>
usingnamespace 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 = newint[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(); }
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.
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.
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).