I am working on this c++ app for class and have hit a wall.

I have been trying to get the average to display with decimal points for accuracy but have had no luck. Here is the code.

Thanks for any help.

//*************************************************************************
//                  Include Files
//*************************************************************************
#include <iostream>
#include <iomanip>
using namespace std;
//*************************************************************************
//                  Function Prototypes follow
//*************************************************************************
int getnumEmpl();
int getdaysOff (int);
double getavg(int, int);
//*************************************************************************
//                 Variable declarations
//*************************************************************************
int numemployees;
int totalDays;
double avg;
//*************************************************************************
//                 Main
//*************************************************************************
int main()
{
    numemployees = getnumEmpl (); 
    totalDays = getdaysOff (numemployees);
    avg = getavg (numemployees, totalDays);
    cout << "Average days off per employee is: " << avg;
return 0;
}            
//*************************************************************************
//                  Function Declarations
//*************************************************************************

int getnumEmpl()
{
    int employees;
    cout << "How many employees do you have on staff?\n";
        cin >> employees;
        
        
        while (employees <= 1)
        {
              cout << "You should enter at least 2 employees\n";
              cout << "to average.\n";
              cout << "How many employees do you have on staff? ";
              cin >> employees;
                  } // end while

                    return employees;
                                     };

int getdaysOff (int numb)
{
    int employeenum, daysOff, totaldaysoff;
    employeenum = 0;
    totaldaysoff = 0;

    while (employeenum < numb) {
      employeenum ++;
      cout << "How many days off has employee number " << employeenum << " taken? ";
      cin >> daysOff;
      
      
      while (daysOff <= 0)
      {
            cout << "Please enter a positive number: ";
            cin >> daysOff;
            } // end while


      totaldaysoff += daysOff;
      }
       
       return totaldaysoff;
};


double getavg(int numemployees, int totaldayoff)
       
{
       
       int daysOffAvg;
       daysOffAvg = totaldayoff / numemployees; 
       
       
return daysOffAvg;
}
//*************************************************************************
//                  Program Output
//*************************************************************************

Recommended Answers

All 13 Replies

I guess I should have added that I have entered
<< fixed << showpoint << setprecision(2);

several times to no avail.

The problem is that you're using integer division, which returns an integer value. Cast the numerator to a double and it should work: daysOffAvg = (double)totaldayoff / numemployees;

Also, try to write your program without using global variables. Their use is considered very poor style.

Oh, and please use code tags when you post your code. :icon_wink:

Thanks for your reply. I keep trying to use local variables but with no luck how would you proceed? Do I need to have each variable in the function? thanks.

Member Avatar for iamthwee

Local functions are local, so you're thinking right. Google it.

All right, here is were I am and I am stuck. Did I mention that I am a newbie?

Program is supposed to ask for number of employees then ask for days off per employee number entered and finally give me an average without using global variables. I believe i have gotten rid of the global variables but have destroyed my code in the process.

Any help would be greatly appreciated.

//*************************************************************************
//                  Include Files
//*************************************************************************
#include <iostream>
#include <iomanip>
using namespace std;
//*************************************************************************
//                  Function Prototypes follow
//*************************************************************************
void getnumEmpl(int);
void getdaysOff (int);
void divide(double, double);
//*************************************************************************
//                 Variable declarations
//*************************************************************************
//int numemployees;
//int totalDays;
//double avg;
//*************************************************************************
//                 Main
//*************************************************************************
int main()
{
    void getnumEmpl(int);
    void getdaysOff (int);
    void divide(double, double) 
    
    return 0;
}            
//*************************************************************************
//                  Function Declarations
//*************************************************************************

void getnumEmpl(int num)
{
    int employees;
    cout << "How many employees do you have on staff?\n";
    cin >> employees;
        
        
        while (employees <= 1)
        {
              cout << "You should enter at least 2 employees\n";
              cout << "to average.\n";
              cout << "How many employees do you have on staff? ";
              cin >> employees;
                  }
}

void getdaysOff (int numb)
{
    int employeenum, daysOff;
 

    while (employeenum < numb) {
      employeenum ++;
      cout << "How many days off has employee number " << employeenum << " taken? ";
      cin >> daysOff;
      
      
      while (daysOff <= 0)
      {
            cout << "Please enter a positive number: ";
            cin >> daysOff;
            } // end while
}


void divide (double totaldaysoff, double employees)
{
     cout << " Days off per employee is: " << totaldaysoff / employees) << endl;
}
//*************************************************************************
//                  Program Output
//*************************************************************************
Member Avatar for iamthwee

I don't mean to sound rude, but do you not have notes. I don't envy the job of explaining the absolute basics.

Basically I am on the right track?

Member Avatar for iamthwee

Well I've not looked at it extensively but for starters, you need to pass actual stuff in main:

int main()
{
     getnumEmpl( something);
     getdaysOff ( something);
     divide(something, something) 
    
    return 0;
}

ok. I have looked over my notes and this seems to be functioning. I cannot however get the division function to work.

Any suggestions??

//*************************************************************************
//                  Include Files
//*************************************************************************
#include <iostream>
#include <iomanip>
using namespace std;
//*************************************************************************
//                  Function Prototypes follow
//*************************************************************************
int first();
int second();
double quotient(int, int);
//*************************************************************************
//                 Variable declarations
//*************************************************************************

//*************************************************************************
//                 Main
//*************************************************************************
int main()
{                   
    int a, b;
    first();
    second();
    quotient(a, b);

    return 0;
}            
//*************************************************************************
//                  Function Declarations
//*************************************************************************

int first()
{
     int employees;
     cout << "How many employees work here? ";
     cin >> employees;
     cout << "Number of employees is: " << employees << endl;
     while (employees < 2.0)
     {
                   cout << "Enter at least 2 employees to average!! ";
                   cin >> employees;
     }
     return 0;
}

int second()
{
     int days;
     
     cout << "How many days off has each employee taken? ";
     cin >> days;
     cout << "Number of days off is: " << days << endl;
     while (days < 0)
     {
                   cout << "Please enter a positive number: ";
                   cin >> days;
     }
     return 0;
}

double quotient(int a, int b)
{
    int r, employees, days;
    a = employees;
    b = days;
    r=b/a;
    cout << "Days off average is " << r << endl;
    return 0;
}

ok. I have looked over my notes and this seems to be functioning. I cannot however get the division function to work.

Any suggestions??

//*************************************************************************
//                  Include Files
//*************************************************************************
#include <iostream>
#include <iomanip>
using namespace std;
//*************************************************************************
//                  Function Prototypes follow
//*************************************************************************
int first();
int second();
double quotient(int, int);
//*************************************************************************
//                 Variable declarations
//*************************************************************************

//*************************************************************************
//                 Main
//*************************************************************************
int main()
{                   
    int a, b;
    first();
    second();
    quotient(a, b);

    return 0;
}            
//*************************************************************************
//                  Function Declarations
//*************************************************************************

int first()
{
     int employees;
     cout << "How many employees work here? ";
     cin >> employees;
     cout << "Number of employees is: " << employees << endl;
     while (employees < 2.0)
     {
                   cout << "Enter at least 2 employees to average!! ";
                   cin >> employees;
     }
     return 0;
}

int second()
{
     int days;
     
     cout << "How many days off has each employee taken? ";
     cin >> days;
     cout << "Number of days off is: " << days << endl;
     while (days < 0)
     {
                   cout << "Please enter a positive number: ";
                   cin >> days;
     }
     return 0;
}

double quotient(int a, int b)
{
    int r, employees, days;
    a = employees;
    b = days;
    r=b/a;
    cout << "Days off average is " << r << endl;
    return 0;
}

i don't understand why initialize employees and days, since you already have those values called from the main function...
i also don't understand why making this function a double if you are returning 0...
this is basic... i wonder how bad your teacher is or how good are you taking notes... it should be

double quotient(int a,int b){
   double r;
   r=b/a;
   cout<<"Days off average is "<<r;
   return r;
}

Please

Please

Please

Don't bump your posts. It is agains the forum rules. 3 hours is not a long time on a forum. We'll get to you when the rest of our lives settle down....

In your functions:

int first()
{
     int employees;
     cout << "How many employees work here? ";
     cin >> employees;
     cout << "Number of employees is: " << employees << endl;
     while (employees < 2.0)
     {
                   cout << "Enter at least 2 employees to average!! ";
                   cin >> employees;
     }
     return 0;
}

So you've just entered the number of employees. But you don't return the value, so it is lost the moment the function returns. Same with the function second() .

And this function:

double quotient(int a, int b)
{
    int r, employees, days;
    a = employees;
    b = days;
    r=b/a;
    cout << "Days off average is " << r << endl;
    return 0;
}

is all wrong. Assignments are backwards and will never work as written. Check your notes again.

actually, i've already solved that problem (most of the times i wouldn't have, but i was in a good mood :D)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.