Hi, I'm trying to obtain the data for the last employee Bob Hu. But for some reason, it doesn’t want to show up. Does anyone know what I'm doing wrong?
This is my code.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
void find_pay(string x)
{
ifstream inputFile;
string filename;
// Get the filename from the user.
cout << "Enter the filename: ";
cin >> filename;
// Open the file.
inputFile.open(filename);
// If the file successfully opened, process it.
if(inputFile.is_open())
{
string data_record,prev,data_name,data_hour;
double total = 0;
while(getline(inputFile,data_record))
{
data_name = data_record.substr(0,30);
data_hour = data_record.substr(31,2);
if(prev.compare(data_name)!= 0 )
{
//Calculate the gross pay, tax, net pay
float gross_pay = 18 * total;
float tax = (12 * gross_pay)/100;
float net_pay = gross_pay - tax;
if(total!= 0)
cout << "Employee:" << prev << "\n""Total Hours: " << total << " Gross Pay: $ " << gross_pay << " Tax " << tax << " Net Pay: " << net_pay << endl;
data_name = data_record.substr(0,30);
prev = data_name;
total = 0;
}
elseif(prev.compare(data_name)==0)
{
total += stod(data_hour);
}
}
//close the file
inputFile.close();
}
}
int main()
{
string filename;
find_pay(filename); //call the void
return 0;
}
This is the output that I got, as you see Bob Hu is missing
1 2 3 4 5 6 7 8 9 10
enter the filename: employeehours.txt
Employee:Jimmy Bucket
Total Hours: 34 Gross Pay: $ 612 Tax 73.44 Net Pay: 538.56
Employee:John Doe
Total Hours: 40 Gross Pay: $ 720 Tax 86.4 Net Pay: 633.6
Employee:Ann Doe
Total Hours: 25 Gross Pay: $ 450 Tax 54 Net Pay: 396
Employee:Mary Jones
Total Hours: 20 Gross Pay: $ 360 Tax 43.2 Net Pay: 316.8
Program ended with exit code: 0
Jimmy Bucket 8
Jimmy Bucket 9
Jimmy Bucket 10
Jimmy Bucket 7
John Doe 8
John Doe 8
John Doe 8
John Doe 8
John Doe 8
Ann Doe 5
Ann Doe 5
Ann Doe 5
Ann Doe 5
Ann Doe 5
Mary Jones 4
Mary Jones 4
Mary Jones 4
Mary Jones 4
Mary Jones 4
Bob Hu 8
Bob Hu 8
Bob Hu 8
Bob Hu 8
Bob Hu 8
That does not fit with the layout of the data provided. That code assumes that the file data is in fixed format layout with the name always the first 30 chars and the hour always the next 2 chars. But the file layout is not this. It is first_name space last_name space hour.
Employee:Ann Doe
Total Hours: 25 Gross Pay: $ 450 Tax 54 Net Pay: 396
Employee:Bob Hu
Total Hours: 40 Gross Pay: $ 720 Tax 86.4 Net Pay: 633.6
Employee:Jimmy Bucket
Total Hours: 34 Gross Pay: $ 612 Tax 73.44 Net Pay: 538.56
Employee:John Doe
Total Hours: 40 Gross Pay: $ 720 Tax 86.4 Net Pay: 633.6
Employee:Mary Jones
Total Hours: 20 Gross Pay: $ 360 Tax 43.2 Net Pay: 316.8
Thank you for your comment, could you help me a little bit more about how the data that you point out doesn't fit with the layout of the data provided.
Also, thank you for sharing your code, however, I just started learning c++ and I don't know much of it. And I could help but notice that your output is missing BOB HUB data.
I need help finding out why the data for Bob Hub is not showing.
There is no data for BOB HUB. There is data for Bob Hu which my output above does show! Data for all 5 names is shown.
how the data that you point out doesn't fit with the layout of the data provided.
The data format as provided above has been changed since my post regarding that. The data above is now in the format expected by your program - and not as originally posted as space-delimited.
I need help finding out why the data for Bob Hub is not showing.
When the end of the file is reached, you need code to display the last employee red data. Data is only displayed when .compare() 1= 0 - which doesn't happen when the end of file is reached. Put the calculation/display code into a function() and call it where you currently calculate/display the data and also after while() terminates.
while (getline()) {
if (current name != previous name) {
print data for previous name;
}
}
In other words, you don't print the data for an employee until you read the next employee. When reading Bob Hu, there is no next employee. So you need to print his data after the loop: