1

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;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Anis Amira
  • 43
  • 7
  • 1
    `cin >> employee[i].empName;` could be a bug. Do the names include only a first name? Remember that `cin >> employee[i].empName;` reads up to the first whitespace character. – drescherjm Nov 02 '21 at 14:11
  • 1
    `double gross = employee[i].chargeHour * employee[i].workingHours;` maybe you wanted to make use of the array of the same name you declared above but did not use. – drescherjm Nov 02 '21 at 14:14
  • Yeaa, I just noticed that. Thank you so much for helping me. It works! – Anis Amira Nov 02 '21 at 15:45
  • I added 0 to initialise the variable. So I guess not every variable needs to initialise the value, right? – Anis Amira Nov 02 '21 at 15:50
  • If you are going to use it you better initialize it otherwise you have undefined behavior. But why add 0 to a number? `sum` is always 0. – drescherjm Nov 02 '21 at 15:53
  • Oh, I see what you meant to do. The answer explains what you need to change to fix the total. – drescherjm Nov 02 '21 at 15:54
  • Another question from me. "cin >> employee[i].empName; reads up to the first whitespace character" - can you explain this to me? Because my lecturer never mention it to his students about it before. So I tried to run the program by putting first and last name and surprisingly, the information turned out incorrect. – Anis Amira Nov 02 '21 at 16:02
  • Related: [https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces](https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces) and after you switch to use std::getline to read a name please read this: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – drescherjm Nov 02 '21 at 16:08
  • See the answer from chris in this question for a better explanation: [https://stackoverflow.com/questions/11462021/issue-with-cin-when-spaces-are-inputted-using-string-class](https://stackoverflow.com/questions/11462021/issue-with-cin-when-spaces-are-inputted-using-string-class) – drescherjm Nov 02 '21 at 16:11

1 Answers1

3

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?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335