0

so i have to make three functions.One asks for how many employees are in a company.The other asks how many days they missed.The third one calculates the average by dividing the amount of employees by the number of days missing.In main,all i have to do is have cout prompts and call the functions.Im not to sure if i'm doing it right,but it crashes when it has to calculate the average.

#include <iostream>
#include <iomanip>
using namespace std;

int employee(int employeeNum);
int missingDays(int daysMissing);
double getAvg(int employeeNum,int daysMissing,double average);

int employee(int employeeNum)
{
  cout<<"Enter the number of employee in the company.";
  cin>>employeeNum;
  return employeeNum;
}

int missingDays(int daysMissing)
{
  cout<<"Enter the amount of days employees missed during the past year.";
  cin>>daysMissing;
  return daysMissing;
}

double getAvg(int employeeNum,int daysMissing,double average)
{
  average=employeeNum/daysMissing;
  return average;
}

int main()
{
  int employeeNum,people,missing,daysMissing;
  double avg,average;

  people=employee(employeeNum);
  cout<<"The number of employees in the company is "<<people<<"."<<endl;

  missing=missingDays(daysMissing);
  cout<<"The number of days employees missed during the past year is "<<missing<<".";

  avg=getAvg(employeeNum,daysMissing,average);
  cout<<average;
}

Let me know what i got to do and thanks for the help.

4
  • 2
    Does it crash if daysMissing is 0? Otherwise add more infos Commented Oct 21, 2013 at 15:38
  • As Vlad pointed out, it should crash if daysMissing is 0. You probably want to return 0 if daysMissing is 0, and otherwise calculate the average. Commented Oct 21, 2013 at 15:39
  • if you could indent your code, that's be great :) Commented Oct 21, 2013 at 15:40
  • 2
    Why are you passing in (by value) employeeNum, daysMissing and average when these are calculated in the functions. Commented Oct 21, 2013 at 15:44

4 Answers 4

2

This looks like homework, so you should only expect nudges. So, some nudges: why do your functions employee() and missingDays() take arguments? And why does getAvg() take 3? In which variables do your input values end up? Where are they used next (if at all)?

Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is this line

missing=missingDays(daysMissing);

for some reason you have two variables for "daysMissing" (I'm not sure why you pass it in to your missingDays function in the first place)

After that line, missing will contain the value input, not daysMissing (since it's not passed by reference)

Your getAvg function, presumably, is crashing with a division by zero error.

Comments

0

So depending on compiler the value of daysMissing is either undefined or initialized with 0.

In your function getAvg you divide employeeNum/daysMissing;

But you are not allowed to divide by zero, thats's it :)

Comments

0
#include <iostream>
#include <iomanip>
using namespace std;

int employee(int employeeNum);
int missingDays(int daysMissing);
double getAvg(int employeeNum,int daysMissing,double average);

int employee(int employeeNum)
{
  cout<<"Enter the number of employee in the company: ";
  cin>>employeeNum;
  return employeeNum;
}

int missingDays(int daysMissing)
{
  cout<<"Enter the amount of days employees missed during the past year: ";
  cin>>daysMissing;
  return daysMissing;
}

double getAvg(int employeeNum,int daysMissing)
{
    if(daysMissing == 0)
    {
        return 0;
    }
  return (double)employeeNum/daysMissing;;
}

int main()
{
  int employeeNum,people,missing,daysMissing;
  double avg,average;

  people=employee(employeeNum);
  cout<<"The number of employees in the company is: "<<people<<"\n";

  missing=missingDays(daysMissing);
  cout<<"The number of days employees missed during the past year is: "<<missing<<"\n";

  avg=getAvg(people,missing);
  cout<<avg;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.