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;
}
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.
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:
#include <iostream>
usingnamespace 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;
}