-2

I feel dumb for not figuring it out and i've been looking around but i cant find a solution. Im using an example that was included with this project. I'm also pretty new to C++ so please excuse any noobish mistakes i might of made.

#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
    string name;
    char line[50];                      // Variable to read data


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

    // test if data file opened incorrectly
    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->regPay = 40 * 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");
}

2 Answers2

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

You should provide absolute path to your file. I think that is the reason for your reported issue. If you prefer to keep the file path as a configurable component, you have to define a environment variable to keep the file destination and use it in program while reading the file.

Moreover don't use Check end of file check in a while loop, it is more error prone since system set the eof bit after read the last line, not while reading the last line. Please refer more details here : Why is iostream::eof inside a loop condition considered wrong?

Instead using eof check in while loop always better to use getline function.

Community
  • 1
  • 1
Steephen
  • 11,597
  • 6
  • 29
  • 41
  • Thank you very much. Do forgive me but it has to be accessible by another user on a different computer. That's why using the full file path wont work. I've tried adding it as an existing item (Which is why inputFile.open("payroll.txt"); reads like that) – Miguel Mendez Jun 17 '15 at 03:25
0
 inputFile.open("payroll.txt");

Make sure you have payroll.txt file where your exe resides. or Provide absolute path with '\\' like

"C:\\Data\\payroll.txt"
Digital_Reality
  • 3,805
  • 1
  • 19
  • 28