Import/Export

I'm going to be working on the following project tonight, and while I don't remember seeing the text about doing this, I'm assuming I can take the information out of two files, save them as temp, then if temp1=temp2 import the hours worked. Just wanted to throw that out there before I start coding. Just wanting to make sure I'm on the right path before I dive in.

Problem:
Assume there are two input files for a payroll program. The first contains the employee’s name (a string), id number (an integer) an hourly rate (a real number). The second contains the employee’s id number and hours worked (a real number). The records in the first file are ordered by name and the records in the second file are ordered by id number. If a person did not work during the current time period, there will be no record in the second file for that person. Write a file of payroll records to an output file for all employees. Each record should contain the employee name followed by # followed by the hours worked, hourly rate, and pay amount. Use a space as a separator between items in the output file. There should be a record in the output file for every person including those who did not work in that pay period.
I'm pretty happy with this (I think, I need to check the numbers still) but for some reason endl isn't working and all the information is running together. Any tips on getting the next iteration to go to the next line?

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
#include <fstream>        //required for file streams
#include <iostream>
#include <cstdlib>        //for definition of EXIT_FAILURE


using namespace std;


#define inFileEmp "employees.txt"    //employee file
#define inFileHour "hours.txt"        //hour file
#define outFile "pay.txt"        //payroll file


//Functions used
void processEmp (ifstream&, ifstream&, ofstream&);    //process all employees and display name, pay, etc


int main()
{
    ifstream eds;        //input: employee data stream
    ifstream hrds;        //input: hour data stream
    ofstream pds;        //output: all info per employee


    //Prepare files
    eds.open(inFileEmp);
    if (eds.fail())
    {
        cerr << "*** ERROR: Cannot open " << inFileEmp << " for input." << endl;
        return EXIT_FAILURE;    //failure return
    }
    
    
    hrds.open(inFileHour);
    if (hrds.fail())
    {
        cerr << "*** ERROR: Cannot open " << inFileHour << " for input." << endl;
        return EXIT_FAILURE;    //failure return
    }
    
    
    pds.open(outFile);
    if (pds.fail())
    {
        cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl;
        return EXIT_FAILURE;    //failure return
    }
    
    processEmp(eds,
        hrds,
        pds);


    //Close files
    eds.close();
    hrds.close();
    pds.close();
    
    return 0;
}


void processEmp
    (ifstream& eds,
    ifstream& hrds,
    ofstream& pds)
{
    string name;        //input: employee name from inFileEmp
    int id1;        //input: employee id from inFileEmp
    float rate;        //input: employee pay rate from inFileEmp
    int id2;        //input: employee id from inFileHour
    int hours;        //input: employee hours worked from inFileHour
    float pay;        //output: pay
        
    eds >> name >> id1 >> rate;
    while (!eds.eof())
    {
        hrds >> id2 >> hours;
        if (id1 == id2)
        {
            pay = rate * hours;
            pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
            eds >> name >> id1 >> rate;
        }
        else if (id1 != id2)
        {
            hrds >> id2 >> hours;
            if (hrds.eof())
            {
                pay = 0;
                pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
                eds >> name >> id1 >> rate;
            }
        }
    }
}




Problem:
Assume there are two input files for a payroll program. The first contains the employee’s name (a string), id number (an integer) an hourly rate (a real number). The second contains the employee’s id number and hours worked (a real number). The records in the first file are ordered by name and the records in the second file are ordered by id number. If a person did not work during the current time period, there will be no record in the second file for that person. Write a file of payroll records to an output file for all employees. Each record should contain the employee name followed by # followed by the hours worked, hourly rate, and pay amount. Use a space as a separator between items in the output file. There should be a record in the output file for every person including those who did not work in that pay period.
Apparently I was way off the mark. Below is the code I've ended up with, but I have in no way accomplished the project. Exhausted and fed up, will try to tackle again tomorrow.

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
#include <fstream>		//required for file streams
#include <iostream>
#include <cstdlib>		//for definition of EXIT_FAILURE

using namespace std;

#define inFileEmp "employees.txt"	//employee file
#define inFileHour "hours.txt"		//hour file
#define outFile "pay.txt"		//payroll file

//Functions used
void processEmp (ifstream&, ifstream&, ofstream&);	//process all employees and display name, pay, etc

int main()
{
	ifstream eds;		//input: employee data stream
	ifstream hrds;		//input: hour data stream
	ofstream pds;		//output: all info per employee

	//Prepare files
	eds.open(inFileEmp);
	if (eds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileEmp << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	hrds.open(inFileHour);
	if (hrds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileHour << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	pds.open(outFile);
	if (pds.fail())
	{
		cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	processEmp(eds,
		hrds,
		pds);

	//Close files
	eds.close();
	hrds.close();
	pds.close();
	
	return 0;
}

void processEmp
	(ifstream& eds,
	ifstream& hrds,
	ofstream& pds)
{
	string name;		//input: employee name from inFileEmp
	int id1;		//input: employee id from inFileEmp
	float rate;		//input: employee pay rate from inFileEmp
	int id2;		//input: employee id from inFileHour
	int hours;		//input: employee hours worked from inFileHour
	float pay;		//output: pay
	int noHours = 0;
		
	
	hrds >> id2 >> hours;
	while (!hrds.eof())
	{
		eds >> name >> id1 >> rate;
		pay = rate * hours;
		if (id1 == id2)
		{
			pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
			hrds >> id2 >> hours;
		}
		else if (id1 != id2)
		{
			eds >> name >> id1 >> rate;
		}
		else (eds.eof());
		{
			pds << name << " " << id1 << " " << rate << " " << hours << " $" << noHours << endl;
			hrds >> id2 >> hours;
		}

	}
}


For those interested, the project is as stated below.

Project:
Assume there are two input files for a payroll program. The first contains the employee’s name (a string), id number (an integer) an hourly rate (a real number). The second contains the employee’s id number and hours worked (a real number). The records in the first file are ordered by name and the records in the second file are ordered by id number. If a person did not work during the current time period, there will be no record in the second file for that person. Write a file of payroll records to an output file for all employees. Each record should contain the employee name followed by # followed by the hours worked, hourly rate, and pay amount. Use a space as a separator between items in the output file. There should be a record in the output file for every person including those who did not work in that pay period.

Files given:

employees.txt
Andrea 1541 7.27
Barry 3794 9.64
Chantal 6260 9.39
Dorian 4740 9.20
Erin 7692 9.18
Fernand 4360 7.14
Gabrielle 2442 8.74
Humberto 5669 9.39
Ingrid 6512 9.16
Jerry 5255 8.08

hours.txt
1541 47
6260 36
4740 37
7692 23
4360 34
2442 25
5669 45
5255 49
Someone else posted a similar (same?) problem a couple days ago.
http://www.cplusplus.com/forum/beginner/73932/

I still suggest using a map, and reading the files one at a time. Is there some reason that you can't do that?

If you can't use the templates and must only use loops, then this is probably going to be needed:
http://www.cplusplus.com/reference/iostream/istream/seekg/
I see that, I'd jump to that thread, but it seems to have never been resolved. I don't want to cross post though, so I'll jump on that strand, see what I can do.
Topic archived. No new replies allowed.