Calculating total pay to employee from hours worked and pay rate.

Thank you for all the help yesterday, i took the advice and made an array and my program works with reading all the files which is good. The issue i cant move past is there are two files a timesheet file and a hourlypay file. You have to match the paycode in the time sheet file to the paycode in the hourly pay file and then take the pay rate from the hourly pay times the hours worked in the time sheet file and im stuck on how to do that. I did it the best i could but im gettig -0.00 for the pay amount which is incorrect. The file info is at the bottom of the post and thank you again for any help on this.
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
63
64
65
66
67
68
69
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
int main()
{
    int empID[70];
    string last[70];
    string first[70];
    string social[70];
    string payCode[70];
    double hoursWorked[70];

    string payCodeH;
    string payRate;

    int timeSheetCnt = 0;
    int hourlyPayCnt = 0;
    int timeSheetPos;
    double payAmt;
    double totalPay;

    ifstream timeSheet;
    ifstream hourlyPay;

    timeSheet.open("TimeSheet.txt");
    hourlyPay.open("HourlyPay.txt");

    if (!timeSheet || !hourlyPay)
    {
        cout << "Error opening file.";
        return -1;
    }
    timeSheet >> empID[timeSheetCnt] >> last[timeSheetCnt] >> first[timeSheetCnt] >> social[timeSheetCnt] >> payCode[timeSheetCnt] >> hoursWorked[timeSheetCnt];
    while (!timeSheet.eof())
    {
        timeSheetCnt++;
        timeSheet >> empID[timeSheetCnt] >> last[timeSheetCnt] >> first[timeSheetCnt] >> social[timeSheetCnt] >> payCode[timeSheetCnt] >> hoursWorked[timeSheetCnt];
    }
    timeSheet.close();

    cout << fixed << showpoint << setprecision(2);
    hourlyPay >> payCodeH;
    while (!hourlyPay.eof())
    {
        hourlyPay >> payCodeH >> payRate;
        timeSheetCnt++;

        for (timeSheetPos =0; timeSheetPos < timeSheetCnt; timeSheetPos++)
        {
            if (payCode[timeSheetCnt] == payCodeH)
            {
                payRate = payAmt;
            }
        }

    }
    totalPay = payAmt * hoursWorked[timeSheetCnt];
    cout << totalPay << endl;


    hourlyPay.close();
    return 0;


}


here is all the info from the txt files.

TimeSheet.txt
10948 Carpenter Stewart 503-21-3387 L31 40.7
10949 Pleasance Marie 433-82-7832 L27 35
10950 Dewey Susan 455-36-8299 L22 21
10951 Ahrens Christopher 488-23-7267 L27 44
10952 Brown Harvey 512-89-8328 L18 41
10953 Roe William 422-91-8282 L23 40
10954 Fuchs Marla 637-27-2878 L21 44
10955 Phelps Gus 284-78-3884 L31 39.5
10956 Genoa Vernon 466-37-8328 L22 41.2
10957 Henderson George 474-78-3873 L19 52
10958 Harness Janet 578-32-7823 L24 32
10959 Harris Wilmer 472-73-7237 L27 48
10960 Halls Melinda 426-76-7127 L29 40
10961 Garner Harriet 632-21-5125 L31 40
10962 Hind Roberta 313-28-7327 L34 42.5
10963 Mukerjee Vitjay 465-35-3255 L21 43
10964 Marino Elizabeth 472-38-7848 L18 37.5
10965 Pemberton Ronald 535-32-5623 L32 30
10966 Porter Loring 923-72-3877 L26 18
10967 Jones Robert 437-32-7828 L28 40
10968 Renner Jonathan 251-25-6562 L24 44
10969 Switzer Jesse 474-78-8732 L31 41.2
10970 Rutherford Martin 378-73-8787 L35 40
10971 Summers Alicia 484-89-4894 L31 40
10972 Smith Mary 489-48-9439 L29 34.5
10973 Rensen Lawrence 452-35-6632 L19 20
10974 Reynard-Hobbs Melva 472-78-3287 L20 40
10975 Jackson Ulrich 538-93-2892 L22 43
10976 Horvath James 723-78-2378 L18 45.9
10977 Williams Francis 573-78-2378 L31 42
10978 Wood Melanie 484-89-8943 L25 40
10979 Williams Horace 436-23-6763 L29 41
10980 Abend Esther 410-38-9002 L31 38
10981 Gregory Jerry 429-39-8299 L22 40
10982 Wood Eleanor 343-87-8778 L20 40
10983 Stark Ellen 585-98-5985 L30 43
10984 Simmons Mary 484-89-9849 L34 45.2
10985 Spears Roberta 489-32-9829 L18 51
10986 Siemens Frederic 429-82-3982 L20 40
10987 Simmons Elizabeth 489-43-8998 L29 39
10988 Snow Gregory 129-81-9829 L26 40
10989 Simms Martin 428-12-9812 L33 32
10990 Reynolds Martin 536-32-6727 L31 43
10991 Brown Fortney 461-67-7627 L18 45.6
10992 Rodgers Maryanne 483-29-8929 L20 40
10993 Pearson Austin 477-21-5215 L22 40
10994 Denton Sharon 571-27-2178 L26 40
10995 Heinrich Lothar 582-32-3782 L27 38.5
10996 Krimm Jacques 412-61-2761 L19 40
10997 Johnson Terrence 471-32-7812 L30 43
10998 Jarndyce James 411-92-8128 L25 50
10999 Smith William 573-78-3278 L22 40
11000 Hertz Conrad 483-28-9328 L23 40
11001 Higgins Preston 643-99-8398 L23 39
11002 Henderson Maureen 827-17-6126 L27 41.2
11003 Haroldson James 227-12-8728 L28 40.7
11004 Henderson Wilma 473-21-7812 L31 39.8
11005 Hines Katharine 492-48-8387 L32 40
11006 Garrick David 411-22-4242 L19 40
11007 Felder Harriet 599-29-3838 L22 44
11008 Maartens Robert 466-99-2839 L25 47
11009 Williamson Harold 661-12-1244 L27 40
11010 Gragg Justin 411-20-3999 L29 40
11011 Eckstein Rodney 512-31-2312 L34 15
11012 Armbruster Helena 522-13-2212 L30 46
11014 Jamison Leon 678-77-9443 L22 6
11015 Innes Neil 411-22-2121 L28 0
11016 Hannula Reino 211-99-9999 L10 68
11017 Shertz Hannah 905-53-2222 L39 5
11018 Annunciata Giovanni 144-32-2233 L15 27.5

hourlypay.txt
L18 10.55
L19 11.87
L20 12.55
L21 14.07
L22 15.18
L23 16.10
L24 17.45
L25 18.05
L26 18.87
L27 19.55
L28 21.00
L29 22.19
L30 23.34
L31 24.83
L32 25.50
L33 26.75
L34 27.55
L35 29.00

There is an error here reading the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 hourlyPay >> payCodeH;
    while (!hourlyPay.eof())
    {
        hourlyPay >> payCodeH >> payRate;
        timeSheetCnt++;

        for (timeSheetPos =0; timeSheetPos < timeSheetCnt; timeSheetPos++)
        {
            if (payCode[timeSheetCnt] == payCodeH)
            {
                payRate = payAmt;
            }
        }

    }


Consider (not tried)

1
2
3
4
5
6
7
8
9
10
11
12
13
    while (hourlyPay >> payCodeH >> payRate)
    {
        timeSheetCnt++;

        for (timeSheetPos =0; timeSheetPos < timeSheetCnt; timeSheetPos++)
        {
            if (payCode[timeSheetCnt] == payCodeH)
            {
                payRate = payAmt;
            }
        }

    }

Okay i did that and it fixed that issue but now im trying to get it to print out some of my variables just to make sure the program still works and they will not print out. I looked it up and the suggestion was to do cout.flush() but that does not do anything in my program and im lost on how to fix that issue. here is what the end of my code looks like. I even tried printing a cout << "hello world"; and that did nothing also.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  while (!hourlyPay.eof())
    {

        timeSheetCnt++;

        for (timeSheetPos =0; timeSheetPos < timeSheetCnt; timeSheetPos++)
        {
            if (payCode[timeSheetCnt] == payCodeH)
            {
                payRate = payAmt;
            }
        }

    }
    totalPay = payAmt * hoursWorked[timeSheetCnt];
    cout.flush();
    cout << endl << empID << " " << payCode;
Last edited on
Where are you reading from hourlyPay stream? You're testing for .eof (probably wrongly as per previous post), but if hourlyPay.eof() is false, the loop will never terminate.
I think this is what you're after:

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	constexpr size_t maxEmp {70};

	ifstream timeSheet("TimeSheet.txt");
	ifstream hourlyPay("HourlyPay.txt");

	if (!timeSheet.is_open() || !hourlyPay.is_open()) {
		cout << "Error opening file.\n";
		return 1;
	}

	int empID[maxEmp] {};
	string last[maxEmp] {};
	string first[maxEmp] {};
	string social[maxEmp] {};
	string payCode[maxEmp] {};
	double hoursWorked[maxEmp] {};
	size_t timeSheetCnt {};

	while ((timeSheetCnt < maxEmp) && (timeSheet >> empID[timeSheetCnt] >> last[timeSheetCnt] >> first[timeSheetCnt] >> social[timeSheetCnt] >> payCode[timeSheetCnt] >> hoursWorked[timeSheetCnt]))
		++timeSheetCnt;

	string payCodeH;
	double payRate {};
	double totalPay {};

	while (hourlyPay >> payCodeH >> payRate)
		for (size_t timeSheetPos = 0; timeSheetPos < timeSheetCnt; ++timeSheetPos)
			if (payCode[timeSheetPos] == payCodeH) {
				totalPay += payRate * hoursWorked[timeSheetPos];
				break;
			}

	cout << "Total Pay is " << fixed << showpoint << setprecision(2) << totalPay << '\n';
}



Total Pay is 13131.64

Last edited on
Your hourlypay.txt is lacking values for L28, L10, L39 and L15

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * @file	main.cpp
 * @author	blongho
 * 			
 *
 * @brief	Attempt to answer http://cplusplus.com/forum/beginner/274532/#msg1184899 
 *
 */

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

class Employee {
private:
    std::string id;
    std::string firstName;
    std::string lastName;
    std::string securityNumber;
    std::string payScale;
    int hoursWorked{};
    std::map<std::string, double> scaleMap;

    void loadPayScale();

    // Make this public if you want to call it outside
    double getTotalPay() const {
        // return hoursWorked * scaleMap.at(payScale); // crashes if key is not in map

        // Because some keys are not in scaleMap, use this loop so that program ends gracefully
        double scale{};
        for (const auto &p : scaleMap) {
            if (p.first == payScale) {
                scale = p.second;
                break;
            }
        }
        return hoursWorked * scale;
    }

public:
    Employee();

    ~Employee() = default;

    bool readFromFile(std::ifstream &file);

    void printInfo();
};

// ============ main ================
int main() {
    Employee employee;
    std::ifstream fin("TimeSheet.txt");
    int index{};
    if (fin) {
        while (employee.readFromFile(fin)) {
            std::cout << ++index << " : ";
            employee.printInfo();
        }
    }

    return 0;
}

// ================= End of main ============

Employee::Employee() {
    loadPayScale();
}


bool Employee::readFromFile(std::ifstream &file) {
    if (file) {
        std::string line;
        std::getline(file, line);
        std::istringstream ss(line);
        //10948 Carpenter Stewart 503-21-3387 L31 40.7
        if (ss >> id >> firstName >> lastName >> securityNumber >> payScale >> hoursWorked) {
            return true;
        } else return false;
    } else {
        //std::cerr << "Error reading employee data\n";
        return false;
    }
}

void Employee::loadPayScale() {
    std::ifstream file("hourlypay.txt");
    if (file) {
        std::string line, scale;
        double value;
        while (std::getline(file, line)) {
            std::istringstream ss(line);
            while (ss >> scale >> value) {
                scaleMap.insert({scale, value});
                // std::cout << "Scale: " << scale << " value: " << value << std::endl;
            }
        }
    } else {
        std::cerr << "Error reading payscale data\n";
    }
}

void Employee::printInfo() {
    std::cout << "Employee [ id=" << id << ", firstname=" << firstName << ", lastname=" << lastName
              << ", social security number=" << securityNumber << ", payscale=" << payScale << ", hoursWorked="
              << hoursWorked << ", totalWages=" << getTotalPay() << "]\n";
}



1 : Employee [ id=10948, firstname=Carpenter, lastname=Stewart, social security number=503-21-3387, payscale=L31, hoursWorked=40, totalWages=993.2]                                                                                   2 : Employee [ id=10949, firstname=Pleasance, lastname=Marie, social security number=433-82-7832, payscale=L27, hoursWorked=35, totalWages=684.25]                                                                                                                                                             
//
//
//                                                                                
70 : Employee [ id=11018, firstname=Annunciata, lastname=Giovanni, social security number=144-32-2233, payscale=L15, hoursWorked=27, totalWages=0]        


Edit: Changes struct to class to enforce encapsulation
Last edited on
Topic archived. No new replies allowed.