Wrong coopilation


I'm new to using c++ and I have to design and code a C++ program that utilizes a function to calculate the following for each employee from the time record input file employeehours.txt. I'm using void and calling the file on my device. However, I have duplicated code inside the void and in the main function and I don't know how to resolve it. This is what I have so far...



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
#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 tp,prev,name,hour;
            double total=0;
            while(getline(inputFile,tp)){
                name = tp.substr(0,30);
                hour = tp.substr(31,2);
                if(prev.compare(name)!=0 ){
                    float gp = 18*total;
                    float tax = (12*gp)/100;
                    float net = gp - tax;
                    if(total!=0)
                        cout << "Employee: " << prev << "\t\tTotal Hours: " << total << " Gross Pay: $" << gp << " Tax" << tax << " Net Pay: " << net << endl;
                    name = tp.substr(0,30);
                    prev = name;
                    total = 0;
                    total+=stod(hour);
                }
                else if(prev.compare(name)==0){
                    total+=stod(hour);
                }
                
            }
        inputFile.close();
        }
}

int main()
{
    string filename;
       cout << "Enter filename: " << endl;
       cin >> filename;
       find_pay(filename);
   return 0;
}
Last edited on
Can you be a bit more specific as to what the problem is?
It's also hard to diagnose your issue because there we don't know what your file looks like, or what your expected output is.
Last edited on
I'm only guessing what your problem is because you didn't really tell us what it is.

But, look at your total variable.

It is assigned 0 in line 19. Then you calculate and print out gp, tax and net based on that value in line 24 - 28. Then you set it to 0 and increment it by the value derived from hour.

The next time through the loop (line 20) you have total containing the value from the previous line's hour field.

I suspect you want to set total based on the hours in the current line. Maybe remove lines 31 and 32 and add total = stod(hour); after line 22.
See my comments below.
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void
find_pay(string filename)		// pass the filename to find_pay
{
    ifstream inputFile;
    // Open the file.
    inputFile.open(filename);

    // If the file successfully opened, process it.
    if (inputFile.is_open()) {
	string tp, prev, name, hour;
	double total = 0;
	while (getline(inputFile, tp)) {
	    name = tp.substr(0, 30);
	    hour = tp.substr(31, 2);
	    if (prev.size() && prev.compare(name) != 0) { // not true the first time through while loop
		// No sense computing stuff unless you're printing it
		if (total != 0) {
		    float gp = 18 * total;
		    float tax = (12 * gp) / 100;
		    float net = gp - tax;
		    cout << "Employee: " << prev << "\t\tTotal Hours: " << total <<
			" Gross Pay: $" << gp << " Tax" << tax << " Net Pay: " << net <<
			endl;
		}
		prev = name;
		total = 0;
	    }
	    total += stod(hour);
	}

	// Handle the last employee
	if (total != 0) {
	    float gp = 18 * total;
	    float tax = (12 * gp) / 100;
	    float net = gp - tax;
	    cout << "Employee: " << prev << "\t\tTotal Hours: " << total <<
		" Gross Pay: $" << gp << " Tax" << tax << " Net Pay: " << net <<
		endl;
	}
	
	inputFile.close();
    }
}

int
main()
{
    string filename;
    cout << "Enter filename: " << endl;
    cin >> filename;
    find_pay(filename);
    return 0;
}

Woooow, that's what OP meant by "duplicate". I didn't realize the x parameter wasn't being used. Nice catch.
Topic archived. No new replies allowed.