The program runs but I can not get it to add the totals from the three employess. Then when i enter an employee info the totals show after each employee instead of the end. Please help. If you can please explain what I need to do in simple terms. Its my first class and I do not have it down yet, thank you.

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//
//CLASS DECLARATION SECTION
//
class EmployeeClass 
{
public:
    void ImplementCalculations(string EmployeeName, int hours, double wage);
    void DisplayEmployInformation(void);
    void Addsomethingup (void);
    string EmployeeName ;
    int hours ;
    double wage ;
    double basepay ;
    double overtime_hours ;
    double overtime_pay ;
    double overtime_extra ;
    double iTotal_salaries ;
    double iIndividualSalary ;
    int iTotal_hours ;
    int iTotal_OvertimeHours ;
};

int main()
{   system("cls"); 

    cout << "\nWelcome to the Employee Pay Center\n\n" ;

    EmployeeClass Employee [3];
    const int numEmployees = sizeof (Employee) / sizeof (Employee [0]);
    for (int i = 0; i < numEmployees; ++i)


    {
    cout<<"\n\n Enter your employee name: ";
    cin >> Employee[i].EmployeeName;

    cout << "Enter hours worked: ";
    cin >> Employee[i].hours;

    cout << "Enter your hourly wage: ";
    cin >> Employee[i].wage;

    }

    for (int i = 0; i < numEmployees; ++i )
    {
        Employee[i].ImplementCalculations(Employee[i].EmployeeName,Employee[i].hours, Employee[i].wage);
    }

}
void EmployeeClass::ImplementCalculations (string EmployeeName, int hours, double wage)
//Initialize overtime variables
{

    basepay=0.0;
    overtime_hours=0;
    overtime_pay=0.0;
    overtime_extra=0.0;
    iIndividualSalary=0.0;


    if (hours > 40)// more than 40

    {

        basepay = (40 * wage);
        overtime_hours = hours-40;
        overtime_pay = wage * 1.5;
        overtime_extra = overtime_hours*overtime_pay;
        iIndividualSalary = (overtime_extra + basepay);

        DisplayEmployInformation();
    }
    else // less than 40 hours
    {
        basepay=hours*wage;
        iIndividualSalary=basepay;

        DisplayEmployInformation();
    }


} //End of Main Function


void EmployeeClass::DisplayEmployInformation () 

{
// This function displays all the employee output information.

    cout<<"n\n";
    cout<<"Employee Name ...................=" << EmployeeName << endl;

    cout<<"Base Pay ....................=" << basepay << endl;

    cout<<"Hours in Overtime ....................=" << overtime_hours << endl;

    cout<<"Overtime Pay ....................=" << overtime_extra << endl;

    cout<<"Total Pay ....................=" << iIndividualSalary << endl;

    Addsomethingup();

} // END OF Display Employee Information


void EmployeeClass::Addsomethingup ()

{

    iTotal_salaries=0;
    iTotal_hours=0;
    iTotal_OvertimeHours=0;


    {

    }

    cout << "\n\n";

    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%% Total Employee Salaries ..... =" << iTotal_salaries << endl;
    cout << "%%%% Total Employee Hours ........ =" << iTotal_hours << endl;
    cout << "%%%% Total Overtime Hours......... =" << iTotal_OvertimeHours << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;

    system("Pause");




    } // End of function

Recommended Answers

All 5 Replies

There are a few issues with your code.

First, your ImplementCalculations function doesn't need to take those three parameters because they are already part of the class (and object). Also, you should probably name the function a bit better, like "ComputeOvertime" or something like that.

Second, you probably shouldn't call the display function from within the ImplementCalculations function. Keep things simple and separate. Call the ImplementCalculations function from the main function to compute those overtime hours and pay, and then, call the display function from the main function to list the employee information.

Third, to the main problem that you have. Summary information like Total_salaries, Total_hours, and Total_OvertimeHours should not be part of your EmployeeClass because they don't pertain to one individual employee. My recommendation is that you create a second class to store that information and implement the computation of those summaries in that class and also a display function for it. In other words, remove the three aforementioned data members from your EmployeeClass class and also, remove the Addsomethingup function. Then, create a class like this:

class WorkForceSummary {
  public:
    WorkForceSummary(EmployeeClass* employees, int employee_count);

    void Display();

    double total_salaries;
    int total_hours;
    int total_overtime_hours;
};

// ...

WorkForceSummary::WorkForceSummary(EmployeeClass* employees, int employee_count) {

  total_salaries = 0;
  total_hours = 0;
  total_overtime_hours = 0;

  for(int i = 0; i < employee_count; ++i) {
    // insert code here to sum up to employees[i].something
    //  and store those sums in total_salaries, total_hours and total_overtime_hours
  };

};

void WorkForceSummary::Display() {

  cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
  cout << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" << endl;
  cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
  cout << "%%%% Total Employee Salaries ..... =" << total_salaries << endl;
  cout << "%%%% Total Employee Hours ........ =" << total_hours << endl;
  cout << "%%%% Total Overtime Hours......... =" << total_overtime_hours << endl;
  cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
  cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;

};

Then, in your main function, you would have something like this:

int main()
{
  cout << "\nWelcome to the Employee Pay Center\n\n" ;
  EmployeeClass Employee [3];
  const int numEmployees = sizeof (Employee) / sizeof (Employee [0]);
  for (int i = 0; i < numEmployees; ++i)
  {
    cout<<"\n\n Enter your employee name: ";
    cin >> Employee[i].EmployeeName;
    cout << "Enter hours worked: ";
    cin >> Employee[i].hours;
    cout << "Enter your hourly wage: ";
    cin >> Employee[i].wage;
  }

  for (int i = 0; i < numEmployees; ++i )
  {
    Employee[i].ImplementCalculations();
    Employee[i].DisplayEmployInformation();
  }

  WorkForceSummary summary(Employee, numEmployees);
  summary.Display();

  return 0;
};

Hi s0urce. I have only made ​​some changes. Compares with your original code.

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    //
    //CLASS DECLARATION SECTION
    //
    class EmployeeClass
    {
    public:
    void ImplementCalculations(string EmployeeName, int hours, double wage,        const int numEmployees);
    void DisplayEmployInformation(string EmployeeName1,double basepay1,double overtime_hours1,double overtime_extra1, double iIndividualSalary1);
    void Addsomethingup (EmployeeClass *Employee1, const int numEmployees);
    string EmployeeName ;
    int hours ;

    double wage ;
    double basepay ;
    double overtime_hours ;
    double overtime_pay ;
    double overtime_extra ;
    double iTotal_salaries;
    double iIndividualSalary;
    int iTotal_hours;
    double iTotal_OvertimeHours;
    };


    int main(){ 

    system("cls");
    cout << "\nWelcome to the Employee Pay Center\n\n" ;
    EmployeeClass Employee [3];
    const int numEmployees = sizeof (Employee) / sizeof (Employee [0]);

    for (int i = 0; i < numEmployees; ++i){

    cout<<"\n\nEnter your employee name: ";
    cin >> Employee[i].EmployeeName;
    cout << "      Enter hours worked: ";
    cin >> Employee[i].hours;
    cout << "  Enter your hourly wage: ";
    cin >> Employee[i].wage;

    Employee[i].ImplementCalculations(Employee[i].EmployeeName,Employee[i].hours, Employee[i].wage,numEmployees);

    }  

    //Here Any Employee Index

    Employee[0].Addsomethingup(Employee, numEmployees);



}

    void EmployeeClass::ImplementCalculations (string EmployeeName, int hours, double wage,const int numEmployees)
    //Initialize overtime variables
    {
    basepay=0.0;
    overtime_hours=0;
    overtime_pay=0.0;
    overtime_extra=0.0;
    iIndividualSalary=0.0;
    if (hours > 40)// more than 40
    {
    basepay = (40 * wage);
    overtime_hours = hours-40;
    overtime_pay = wage * 1.5;
    overtime_extra = overtime_hours*overtime_pay;
    iIndividualSalary = (overtime_extra + basepay);
    DisplayEmployInformation(EmployeeName,basepay,overtime_hours,overtime_extra,iIndividualSalary);

    }
    else // less than 40 hours
    {
    basepay=hours*wage;
    iIndividualSalary=basepay;
    DisplayEmployInformation(EmployeeName,basepay,overtime_hours,overtime_extra,iIndividualSalary);

    }


    } //End of Main Function


    void EmployeeClass::DisplayEmployInformation (string EmployeeName1,double basepay1,double overtime_hours1,double overtime_extra1,double iIndividualSalary1)
    {
    // This function displays all the employee output information.
    cout<<"\n\n";
    cout<<" Employee Name ...............= " << EmployeeName1 << endl;
    cout<<" Base Pay ....................= " << basepay1 << endl;
    cout<<" Hours in Overtime ...........= " << overtime_hours1 << endl;
    cout<<" Overtime Pay ................= " << overtime_extra1 << endl;
    cout<<" Total Pay ...................= " << iIndividualSalary1 << endl;

    } // END OF Display Employee Information
    void EmployeeClass::Addsomethingup (EmployeeClass *Employee1, const int numEmployees)
    {

         iTotal_salaries = 0;
         iTotal_OvertimeHours = 0;
         iTotal_hours =0;

         for (int a=0; a<numEmployees; a++){

             hours = Employee1[a].hours;

              wage = Employee1[a].wage;


    if (hours > 40)// more than 40
    {
    basepay = (40 * wage);
    overtime_hours = hours-40;
    overtime_pay = wage * 1.5;
    overtime_extra = overtime_hours*overtime_pay;
    iIndividualSalary = (overtime_extra + basepay);

    }
    else // less than 40 hours
    {
    basepay=hours*wage;
    iIndividualSalary=basepay;

    }


    iTotal_salaries+= iIndividualSalary;
    iTotal_OvertimeHours += overtime_hours;
    iTotal_hours += hours;

}


    cout << "\n\n";
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%% Total Employee Salaries ..... =" << iTotal_salaries << endl;
    cout << "%%%% Total Employee Hours ........ =" << iTotal_hours << endl;
    cout << "%%%% Total Overtime Hours......... =" << iTotal_OvertimeHours << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;


    system("Pause");

    } // End of function

Any mistake, let me know. Regards.

commented: We do not FIX other's code. We make suggestions so they can fix their own code and learn how to do it themselves. +0

Thank you for helping me out. I do appreciated it. Daxmark I used your information but I can not get the overtime to work. I put 44 hours and i would get 4 hours of overtime but the over all total would give me 12 hours. I tried to look for the problem but I can not find it. can you give me a hint.

I put 44 hours and i would get 4 hours of overtime but the over all total would give me 12 hours. I tried to look for the problem but I can not find it. can you give me a hint.

Hi s0urce. I forgot to reset the "overtime_hours" to zero in the event that be less than 40 hours, else always has the same value.

Please in the line 125, add:

overtime_hours = 0; 

Again. Any mistake, let me know. Regards.

Thank you very much, with your help I was able to pass my class.

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.