What is wrong with my code?

Below is my code. The objective is to use a function that takes no arguments to ask how many employees are with a company, use a function that takes one argument to ask how many days each employee was gone, then use a function that takes two arguments, that doesn't show any output, to calculate the average of the two. When I try to run the code, it asks twice how many employees there are, then whatever the second number was you typed in, it displays as the average. Can someone help me out?

#include <iostream>
using namespace std;

int employeeNumber();
int absentDays(int a);
double averageDays(double a, double b);

int main(void) {

int numEmployees, numDays;

cout << "Welcome to Days Out Calculator!" << endl;
cout << endl;
numEmployees = employeeNumber();
numDays = absentDays(numEmployees);
cout << "The average number of days each employee was gone is: " << averageDays(numEmployees, numDays);
cout << endl;
cout << "Thank you for playing Days Out Calculator!";

return 0;
}

int employeeNumber() {
int number;
cout << "Number of employees with the company: ";
cin >> number;
while (number < 1) {
cout << "That number is less than 1, so it won't work. Try again: ";
cin >> number;
}
return number;
}

int absentDays(int a) {
int workers, days, total;
workers = a;
total = 0;
for (int count = 0; count > workers; count++) {
cout << "Input the number of days each employee was absent: ";
cin >> days;
total += days;
}
return total;
}

double averageDays(double a, double b) {
double average;
average = (employeeNumber() + absentDays(a) / 2);
return average;
}
Last edited on
for (int count = 0; count > workers; count++) {
should be
for (int count = 0; count < workers; count++) // test is < {
So I'm trying those code now, and for some reason "employeeNumber" is being ran twice and your line of code calling absentDays is not being called. I will keep messing with it.
Yeah. You have a logic error in your for loop in workers absent. You also are calling employeeNumber() again in averageDays().
I believe, if I am understanding your assignment right, this should be your code. I commented the two lines I changed. You were averaging with new employee numbers and new days absent instead of the values you already asked for earlier in the code. You also had count > workers but count started at 0 so it could never run:

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

int employeeNumber();
int absentDays(int a);
double averageDays(double a, double b);

int main() {

int numEmployees, numDays;

cout << "Welcome to Days Out Calculator!" << endl << endl;
numEmployees = employeeNumber();
numDays = absentDays(numEmployees);
cout << "The average number of days each employee was gone is: " << averageDays(numEmployees, numDays);
cout << endl;
cout << "Thank you for playing Days Out Calculator!";

return 0;
}

int employeeNumber() {
int number;
cout << "Number of employees with the company: ";
cin >> number;
while (number < 1) {
cout << "That number is less than 1, so it won't work. Try again: ";
cin >> number;
}
return number;	
}

int absentDays(int a) {
int workers, days, total;
workers = a;
total = 0;
for (int count = 0; count < workers; count++) { //This was wrong
cout << "Input the number of days each employee was absent: ";
cin >> days;
total += days;
}
return total;
}

double averageDays(double a, double b) {
double average;
average = ((a + b) / 2); //This was wrong
return average;
}
Last edited on
Topic archived. No new replies allowed.