Integer division by zero

Trying to find the average number of days absent employees and i keep getting a integer division by zero error. not sure how to correct it but any help would be appreciated!




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

int employee();
int numberdays (int);
double avgnum (int, int);

int employeenumbers, employeenums, numdays;

int main()
{


	
employee();
numberdays (employeenums);
avgnum (employeenums,  numdays);




system("pause");
return 0;
}


int employee ()
{
int employeenums;
cout<<"Enter the number of employees in the company:   "<<endl;
cin>>employeenums;

while (employeenums<1)
{ cout<<"Enter a number greater than 1:     "<<endl;
  cin>> employeenums;
}

cout<<"The number of employees in the company is      "<<setprecision(2)<<fixed<<employeenums<<endl;
return employeenums;
}

int numberdays (int employeenums)
{
	int numdays;
	cout<< "How many days did each employee miss during the past year?"<<endl;
	cin>>numdays;
	while (numdays<0)
	{ cout<<"Enter a non-negative number of days:                     "<<endl;
	  cin>> numdays;
	}
	cout<<"The number of days missed is:                              "<<numdays<<endl;
	return numdays;
}

double avgnum (int employeenums, int numdays)
{
	double avg;
	avg= numdays/employeenums;
	cout<<"the avg is  "<<setprecision(2)<<fixed<<avg<<endl;
	return avg;
}
closed account (28poGNh0)
// In your program you dont use employeenumbers so delete it
// also your functions return but you dont use the values returned ,also if you have a global variables and if you insect using those global variables well you dont need to return anything .Turn the function's prototypes to void or delete the global variables
// int numberdays (int employeenums) dont use the parameter employeenums so delete it
// finnaly your program crashs because you using a local variables that share the same name as your global ones

in fact ,your global variables are not been assinged at all so they will be set to 0 0 or some random values

I suggest this new version

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

int employee(void);
int numberdays(void);
double avgnum(int, int);

int main()
{
    int employeenums,numdays;

    employeenums = employee();
    numdays = numberdays();
    avgnum(employeenums,numdays);

    return 0;
}

int employee(void)
{
    int employeenums;
    cout << "Enter the number of employees in the company:" << endl;
    cin >> employeenums;

    while(employeenums<1)
    {
        cout << "Enter a number greater than 1:     "<<endl;
        cin >> employeenums;
    }

    cout << "The number of employees in the company is      " << setprecision(2) << fixed
    << employeenums << endl;

    return employeenums;
}

int numberdays(void)
{
	int numdays;

	cout << "How many days did each employee miss during the past year?" << endl;
	cin >> numdays;
	while(numdays<0)
	{
	    cout << "Enter a non-negative number of days:                     " << endl;
        cin >> numdays;
	}
	cout << "The number of days missed is:                              " << numdays << endl;

	return numdays;
}

void avgnum (int employeenums, int numdays)
{
	double avg;
	avg = (double)numdays/employeenums;
	cout << "the avg is  " << setprecision(2) << fixed << avg << endl;
}


hope thats helps
Last edited on
Topic archived. No new replies allowed.