Home > Net >  Calculate total gross pay of 10 employees. (C - Array in Struct)
Calculate total gross pay of 10 employees. (C - Array in Struct)

Time:11-03

The result of this question, it should have a payroll record consists all of these things. But i have a problem in calculating the TOTAL GROSS PAY FOR ALL EMPLOYEES by using arrays in struct (C ) but I am stuck. The total gross pay should be printed at bottom of the payroll record. I feel like something is missing in my coding but I can`t figure out what that thing is. I only have a problem in finding the total gross pay, others are okay.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    double gross[10];
    double sum = 0.0;
    double totalGrossPay;
    struct GrossPay {
        int empID;
        string empName;
        double chargeHour;
        int workingHours;
        double grossPay;
    };

    GrossPay employee[10];

    for (int i = 0; i < 10; i  ) {
        cout << (i   1) << "."
             << "Employee Name :";
        cin >> employee[i].empName;

        cout << "Employee ID :";
        cin >> employee[i].empID;

        cout << "Employee`s charge rate per hour :";
        cin >> employee[i].chargeHour;

        cout << "Working hours :";
        cin >> employee[i].workingHours;

        cout << endl;
    }
    cout << "Employee ID\t"
         << "Employee Name\t"
         << "Gross Pay(RM)" << endl;
    for (int i = 0; i < 10; i  ) {
        double gross = employee[i].chargeHour * employee[i].workingHours;
        cout << employee[i].empID << "\t\t" << employee[i].empName << "\t\t" << gross;
        cout << endl;
    }
    cout << endl;

    for (int i = 0; i < 10; i  ) {
        totalGrossPay = sum   gross[i];
    }
    cout << "Total gross pay of 10 employees : RM" << totalGrossPay;
    return 0;
}

CodePudding user response:

You have an uninitialized array

double gross[10];

So its elements have indeterminate values.

As a result this loop

for (int i = 0; i < 10; i  ) {
    totalGrossPay = sum   gross[i];
}

invokes undefined behavior.

Also the variable sum has not changed in the preceding code. So its using in this for loop does not make a sense.

Maybe you mean in the body of the loop

double totalGrossPay = 0.0;

for (int i = 0; i < 10; i  ) {
    totalGrossPay  = gross[i];
}

provided that the array gross is filled with values.

It seems that in this for loop

for (int i = 0; i < 10; i  ) {
    double gross = employee[i].chargeHour * employee[i].workingHours;
    cout << employee[i].empID << "\t\t" << employee[i].empName << "\t\t" << gross;
    cout << endl;
}

you mean elements of the array gross instead of the local variable gross as for example

for (int i = 0; i < 10; i  ) {
    gross[i] = employee[i].chargeHour * employee[i].workingHours;
    cout << employee[i].empID << "\t\t" << employee[i].empName << "\t\t" << gross[i];
    cout << endl;
}

Also the data member double grossPay; of the structure is not used. Maybe instead of the array gross you need to fill this data member of elements of the array of structures do not you?

  • Related