0

I was tasked with completing the following assignment and have currently hit a brick wall:

Write an application (including algorithm) that uses an input file to process the payroll of a company. The file format includes a record for each employee that contains the following information (each field separated by colon) First Name: Last Name: Employee ID: Pay Rate: Hour Worked.The application must read the file of records and store the information in parallel arrays (maximum size of each array will be 100 entries). Therefore the application must be designed to support a file that contains a maximum of 100 employees.
In addition an array, “wages” must be used to store the salary of each employee. The wages are calculated based on the following logic. If Hours Worked <= 40Wage = Pay Rate * Hours Worked Else Wage = Pay Rate * 40 + ((Hours Worked – 40)* Pay Rate * 1.5) The application must display all the array information (tabular format) for each record, in sorted order, based on the employee wages and display the total of all of the employee wages at the bottom of the table. area. Note: The application must read all records of the file by using the technique of testing for the end-of-file (i.e., cannot prompt user for the number of records in the file).

I was given the txt file and all but im struggling to sort it and when I try to make a while loop that detects eof it justs infinitely loops the same data. Here is where i am at:

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main()
{
    ifstream myFile("Assignment4.txt");
    if (!myFile.is_open())
    {
        cout << "File Failed to Open!" << endl;
        return 0;
    }

    string firstname, lastname;
    int id, hours;
    double rate;

    string myString;
    string line;

    while (getline(myFile, line))
    {
        stringstream ss(line);
        getline(ss, firstname, ':');
        getline(ss, lastname, ':');
        getline(ss, myString, ':');
        id = stoi(myString);
        getline(ss, myString, ':');
        rate = stoi(myString);
        getline(ss, myString, ':');
        hours = stoi(myString);

        cout << "First Name |" << " Last Name |" << " Employee ID |" << "Hourly Rate |" << " Hours" << endl;

        while (!myFile.eof())
        {

            cout << setw(10) << firstname << " |" << setw(10) 
                << lastname << " |" << setw(12) 
                << id <<" |"<<  setw(10) << "$" 
                << rate << " |" << setw(6) 
                << hours << endl;
        }
    }
    myFile.close();
}

This is the text file:

Mike:Jones:567:25.75:67
Sue:Smith:45:30.25:82
Ann:Barber:2:15.45:39
Billy:Simpson:1234:10.15:65
Barabara:Stone:75:45.33:22
Alan:Colllins:17:12.75:73
Cindy:Davis:210:13.67:45
Eilein:Ferguson:62:53.36:17
Gordon:Howard:981:9.89:31
Bob:Jones:295:14.73:43

Any Help would be greatly appreciated!

lakeweb
  • 1,727
  • 2
  • 13
  • 17
npt-longo
  • 1
  • 2
  • 1
    Can you please simplify your question into an actual question? What isn't working? – ChrisMM Apr 14 '20 at 15:35
  • 1
    Don't use [stream::eof](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). Your program actually works if you comment the line `while (!myFile.eof())`. – lakeweb Apr 14 '20 at 15:38
  • 2
    The last `while` loop doesn't really make sense. You'd just keep looping over the same data.. – WBuck Apr 14 '20 at 15:39

1 Answers1

1

The easiest way to solve this problem, is create a struct with the data, and sort this struct.

#include <bits/stdc++.h>

using namespace std;

struct employee{
  string firstName, lastName;
  int employeeId, hourWorked;
  double payRate, wage;

  employee() {}

  employee(string _firstName, string _lastName, int _employeeId, double _payRate, int _hourWorked) {
    firstName = _firstName;
    lastName = _lastName;
    employeeId = _employeeId;
    payRate = _payRate;
    hourWorked = _hourWorked;
    if(hourWorked <= 40) 
      wage = payRate*hourWorked;
    else 
      wage = payRate*40 + ((hourWorked - 40) * payRate * 1.5);
  }

  bool operator < (const employee other) const {
    return wage < other.wage;
  }

};

int main() {
  vector <employee> employees;
  // read of file
  // add all the employees to the vector
  employees.push_back(employee(firstName, lastName, employeeId, payRate, hourWorked));
  // after add all the employees, you need to sort the vector
  sort(employees.begin(), employees.end());
  // now you just need to print the answer
  for(int i = 0; i < employees.size(); i++) {
    employee aux = employees[i];
    cout << aux.firstName << ":" << aux.lastName << ":" << aux.employeeId << ":" << aux.payRate << ":" << aux.hourWorked << ":" << aux.wage << endl;
  }

  return 0;
}

If you want to sort decrease order, you just need to change the following code:

bool operator < (const employee other) const {
  return wage < other.wage;
}

To this code:

bool operator < (const employee other) const {
  return wage > other.wage;
}