Quick C++ Function Problem

Hey forum,

I have done all of the assignment so far except for one part. Here is the assignment:

Write a program that calculates the average number of days a company's employees are absent. The program should have the following functions:

(1) A function that asks the user for the number of employees in the company. (no parameters, returns an int).

Example:
"How many employees does your company have?" 3

(2) A function that asks the user to enter the number of days each employee missed during the past year. (1 parameter for number of employees, returns total of days missed for all employees)

Example:
"How many days did Employee #1 miss?" 3
"How many days did Employee #2 miss?" -1
"The number of days absent must be >= 0."
"How many days did Employee #2 miss?" 6
"How many days did Employee #3 miss?" 0

(3) A function to calculate the average number of days absent (2 int parameters: number of employees and number of days, returns float storing average number of days missed.)

Example:
"The average number of days absent is: 3"

Your program should not accept negative numbers as input: use loops to ensure this.

To get full credit you must follow the instructions on how each function should be organized.

I have completed everything except the part where it says that the average number returns float storing average number of days missed. Does this mean that if the average is a decimal the program should output it as a decimal as well and not have it truncate? If so, the problem I'm having seems to only include the whole integer number and no decimals. Here is my code:



#include "stdafx.h"

#include <iostream>

using namespace std;


int EmployeeNumber();

int DaysMissed(int);

float AverageDays(int,int);


int main()

{
    int EmployeeNum;

    int AbsentDays;

    float average;

    
    EmployeeNum = EmployeeNumber();

    AbsentDays = DaysMissed(EmployeeNum);

    average = AverageDays(EmployeeNum, AbsentDays);

    cout << "The average number of days absent is: " << average << endl; 

    system("pause");

    return 0;
}

int EmployeeNumber()

{
    int employeeNum;

    cout << "How many employees does your company have? ";

    cin >> employeeNum;

    while(employeeNum < 1)

    {
        cout << "Please enter a value that is greater than 0. " << endl;

        cin >> employeeNum;
    }

    return employeeNum;
}


int DaysMissed(int employeeNum)

{
    int totalDays = 0;

    int employee;

    for(int counter = 1; counter <= employeeNum; counter++)

    {
		cout << "How many days did employee #" << counter << " miss? ";

        cin >> employee;

        if(employee < 0)

        {
            cout << "Please enter a non-negative number for the days missed. " << endl;

			cout << "How many days did employee #" << counter << " miss? ";

            cin >> employee;
        }

        totalDays += employee;
    }

    return totalDays;
}

float AverageDays(int employeeNum, int totalDays)

{
    float averageDays;

    averageDays = (totalDays / employeeNum);

    return averageDays;

}



What am I doing wrong here? I've spent a couple hours figuring this out haha. Thank you so much for any help. =D
averageDays = (static_cast<float>(totalDays) / employeeNum);// Otherwise it will be integer division here
You can actually simpify your function be getting rid of temp variable:
1
2
3
4
float AverageDays(int employeeNum, int totalDays)
{
   return static_cast<float>(totalDays) / employeeNum;
}
Last edited on
To expand on what MiiNiPaa said, if you divide two variables of type int, the result is an int. So any decimal value is going to be truncated before the result is copied to your float variable.

So, effectively, what your code does is:

1
2
3
4
5
6
7
8
9
10
11
float AverageDays(int employeeNum, int totalDays)
{
    float averageDays;

    int temp = (totalDays / employeeNum);

    averageDays = static_cast<float>(temp);

    return averageDays;

}


To force the division to evaluate to a float value, you need to do as MiiNiPaa suggests, and cast one of the values to a float.

It's an issue that often catches people out.

Last edited on
closed account (3qX21hU5)
I would also recommend changing the paramter to a float also since that is what you are passing it. When you pass in a float into a int parameter it will convert the float into a int (Just removes the decimal) when it makes the temporary copy. This wouldn't be the case if you use a pass by reference but as your function is right this will be the case.

So float AverageDays(int employeeNum, float totalDays); would be better.

Here is a example to show what I mean

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

float stuff(int number)
{
    return static_cast<float>(number);
}

int main()
{
    float fnum = 1.05;
    cout << stuff(fnum);
}


Even with the float cast on the return it will still return the number without a decimal because it was deleted during the copy process in the parameter.
Thanks for the help guys, I managed to get it working! =)
Topic archived. No new replies allowed.