Read the data of the last employee

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <fstream>
using namespace 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;
                    
                }
               
                else if(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


Here is the file where the data is read.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

Last edited on
1
2
data_name = data_record.substr(0,30);
data_hour = data_record.substr(31,2);


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.
This code can be vastly simplified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fstream>
#include <iostream>
#include <string>
#include <map>

struct Rec {
	std::string first;
	std::string last;
	double hours {};
};

int main()
{
	std::ifstream ifs("employeehours.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	std::map<std::string, double> employees;

	for (Rec rec; ifs >> rec.first >> rec.last >> rec.hours; employees[rec.first + ' ' + rec.last] += rec.hours);

	for (const auto& [name, hours] : employees) {
		const auto gross_pay {18 * hours};
		const auto tax {(12 * gross_pay) / 100};
		const auto net_pay {gross_pay - tax};

		std::cout << "\nEmployee:" << name << "\nTotal Hours: " << hours << " Gross Pay: $ " << gross_pay << " Tax " << tax << " Net Pay: " << net_pay << '\n';
	}
}



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.
Last edited on
The code basically says:
1
2
3
4
5
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <fstream>
using namespace 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;
                    
                }
               
                else if(prev.compare(data_name)==0)
                {
                    total += stod(data_hour);
                }
                
            }
	    //Calculate the gross pay, tax, net pay
	    float gross_pay = 18 * total;
	    float tax = (12 * gross_pay)/100;
	    float net_pay = gross_pay - tax;
	    cout << "Employee:" << prev << "\n" "Total Hours: " << total << " Gross Pay: $ " << gross_pay << " Tax " << tax << " Net Pay: " << net_pay << endl;
        //close the file
        inputFile.close();
        }
}

int main()
{
    string filename;
       find_pay(filename); //call the void
   return 0;
}
Topic archived. No new replies allowed.