Q:

C++ Overtime and Payroll Calculations

when you execute my code you'll notice the regular pay for the first example will calculate to $500 when it is only supposed to calculate to $400. I'm also having a problem adding up the totals of the regular pay and overtime pay to get a total payroll which is displayed in one cout statement. 95% of my code is correct, I'm just having these two issues, any help is appreciated. To make it more clear hear is my assignment:
The regular working hour for an employee is 40 hours a week. Sometimes employees work over   time. When the employee works 40 or less hours, the regular pay is calculated as number of hours worked times the hourly rate. When the employee works over time i.e. beyond 40 hours then the employee’s pay has two components a) regular pay (40 hours X hourly rate), and 2) overtime pay (the    overtime hours beyond 40 X time and half of the hourly rate).
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std; 

struct incomeInfo {
    string id;
    string name;
    int hours;
    double hRate;
    double regPay = 0;
    double otPay = 0;
};

const int ARRAY_SIZE = 25; // Array size 
incomeInfo income[ARRAY_SIZE]; // array variable declaration

void getIncome(incomeInfo[], int&);
void compute(incomeInfo *, int);
void display(incomeInfo[], int);
void summary(incomeInfo[], int);

int main()
{

    incomeInfo income[ARRAY_SIZE];
    int count = 0;                          //Initialize count to 0

    getIncome(income, count);
    compute(income, count);
    display(income, count);
    summary(income, count);
    return 0;
}

void getIncome(incomeInfo income[], int &count)
{

    ifstream inputFile;                 // declare input file variable
    char line[50];                      // Variable to read data

    // Open data File to read data
    inputFile.open("Payroll.txt");

    // test if data file opened correctly
    if (inputFile.fail())
    {
        cout << "\n\n\tError openning file: " << "\n\n\t";
        system("pause");
        exit(1);
    }

    else
    {
        while (!inputFile.eof())    //Check end of file
        {
            inputFile.getline(line, 50, ',');       // The data are separated by comma 
            income[count].id = line;
            inputFile.getline(line, 50, ',');
            income[count].name = line;
            inputFile.getline(line, 50, ',');
            income[count].hours = atoi(line);       // Convert string to integer
            inputFile.getline(line, 50, ',');
            income[count].hRate = atof(line);           // Convert string to float

            count++;
        }
    }
    inputFile.close();

    return;
}

void compute(incomeInfo *ptrI, int count)    
{                                                
    for (int i = 0; i<count; i++)

    if (ptrI->hours <= 40)
    {
        ptrI->regPay = ptrI->hours * ptrI->hRate;
        ptrI++;
    }

    else if (ptrI->hours > 40)
    {
        ptrI->regPay = ptrI->hours * ptrI->hRate;
        ptrI->otPay = (ptrI->hours - 40) * (ptrI->hRate + (ptrI->hRate* .5));
        ptrI++;
    }
        return;
}

void display(incomeInfo income[], int count)
{
    cout << fixed << showpoint << setprecision(2);
    cout << setw(15) << left << "ID" << setw(16) << "Name";
    cout << left << setw(8) << "Hours" << setw(14) << "Hourly Rate" << setw(14) << "Regular Pay" << setw(14) << "Overtime Pay" <<endl;

    for (int i = 0; i < count; i++)
    {
        cout << setw(14) << left << income[i].id << setw(15) << income[i].name;
        cout << right << setw(6) << income[i].hours << setw(12) << income[i].hRate;
        cout << setw(14) << income[i].regPay << setw(14) << income[i].otPay << endl;
    }

    return;
}

void summary(incomeInfo income[], int count)
{

    for (int i = 0; i < count; i++)
        cout << endl << endl << "Total payroll amount for the company = " << income[i].regPay + income[i].otPay <<endl;

    system("PAUSE");
}

A:

I think this is the solution you want. In the else if part of compute(), you need to write ptrI->regPay = 40 * ptrI->hRate;. I have commented out your previous line. Also you may use only else instead of else if (ptrI->hours > 40) if you want. Please check and let me know:
void compute(incomeInfo *ptrI, int count)    
{                                                
    for (int i = 0; i<count; i++)
  {
    if (ptrI->hours <= 40)
    {
        ptrI->regPay = ptrI->hours * ptrI->hRate;
        ptrI++;
    }

    else if (ptrI->hours > 40)
    {
                    //ptrI->regPay = ptrI->hours * ptrI->hRate;

        ptrI->regPay = 40 * ptrI->hRate;
        ptrI->otPay = (ptrI->hours - 40) * (ptrI->hRate + (ptrI->hRate* .5));
        ptrI++;
    }
  }
        return;
}

