Stuck in an if statement

I'm working on a program that reads in two files (one containing employee IDs and their hours and another containing employee names, IDs, and pay rate) and writes all the information for each employee into a third file. Right now my main focus is successfully matching up the information and displaying it on the screen. For some reason my function locks up on line 38 and never continues or terminates. It isn't stuck in an infinite loop because the line is inside an if statement. Any feedback is appreciated
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
//Displays employee name, and pay rate from employees.txt and ID and hours from hours.txt
//Appears to be unable to process past line 38. The program will not execute line 39 and does not
//terminate
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define fileName "hours.txt"
#define fileTwo "employees.txt"

 int ID, hours;
 string name;
 int IDTwo;
 float rate;
 int flag;
 ifstream inFile;
 ifstream inFileTwo;
 void openHours(int&, int&); //Both of this functions are not currently active in the main 
 void openEmployees(string&, int&, float&);
    
int main()
{  

    flag = 0;
    inFile.open(fileName);
    inFileTwo.open(fileTwo);
    while (!inFileTwo.eof()) //employees.txt as a event control
    {
    
    inFile >> ID >> hours; 
    inFileTwo >> name >> IDTwo >> rate;
    
     if (ID == IDTwo) //Displays all employee information if the IDs matched without any loops
    {
        cout << "Employee: " << name << " ID: " << ID << " Hours: " << hours 
                << " Pay Rate: " << rate << endl;
         cout << "Stuck here?";
       
    }
    
        else if (ID != IDTwo)
    {
        while ((ID != IDTwo) && (!inFile.eof()));  //Used to match ID numbers between files
        {
            inFile >> ID >> hours;
           
        if (ID == IDTwo) //Displays all employee information
        {
        cout << "Employee: " << name << " ID: " << ID << " Hours: " << hours 
                << " Pay Rate: " << rate << endl;
        } 
            else if ((ID != IDTwo) && (flag !=1) && (inFile.eof()))
        {
            inFile.seekg(0); //Restarts retrieving information from the beggining of hours.txt
            flag = 1;
          
        }
        }
    
    if   ((ID != IDTwo) && (inFileTwo.eof())) //Displays employees with 0 hours
    {
        cout << "Employee: " << name << " ID: " << ID << " Hours: 0 "
                << " Pay Rate: " << rate << endl;
    }
        
    }
   
    flag = 0;
}
    inFile.close();
    inFileTwo.close();
    return 0;
}
    
Hi sparkysnow,

Welcome to cplusplus forums.

The way to do this is to have structs that hold all the fields in their respective files.

Then have an array of structs for each file. Use a loop to read the info into the array.

Have you used the debugger?

Hope this helps

TheIdeasMan
Thanks for the help but I'm new to this. Can you give me an example as to how to do that? I found some in other forums but I am getting a lot of errors.
(my attempt):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
   ifstream inFile;
   
int main()
{
#define fileOne "employees.txt";
#define fileTwo "hours.txt";
    struct employee { 
        string name;
        int ID;
        float rate;
    };
    struct hours {
        int IDTwo;
        int hours;
    };

   inFile.open(fileOne);
   inFile >> employee.name >> employee.ID >> employee.rate;

Errors:
payrollTwo.cpp:22: error: expected `)' before ';' token
payrollTwo.cpp:22: error: expected primary-expression before ')' token
payrollTwo.cpp:22: error: expected `;' before ')' token
payrollTwo.cpp:23: error: expected primary-expression before '.' token
payrollTwo.cpp:23: error: expected primary-expression before '.' token
payrollTwo.cpp:23: error: expected primary-expression before '.' token
make[2]: *** [build/Debug/Cygwin-Windows/payrollTwo.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
OK, you can't use a debugger until you get the code to compile first.

So you have declared some struct types but haven't created any.

You can do either of these:

1
2
3
4
struct hours {
        int IDTwo;
        int hours;
    } HoursRecord, AnotherOne;


OR:

1
2
3
4
5
6
7
struct hours {       //A description of this struct is for
        int IDTwo;    //desription of what this is
        int hours;     //desription of what this is
    } ;
hours HoursRecord;   //A description of how this is used
hours AnotherOne;   //A description of how this one is different


The latter style is better as you can document them as I have shown.

If you are going to use #defines put them after the #include's before main.

I wouldn't reccommend using #defines for file names because that hard codes them - it is better to have them as a variable.

When dealing with filestreams, you should check that things work. Eg open a file, check that it worked - the file might not exist, or you get Permission Denied from OS Security.

You need a loop to read info from the file and put it into the struct.

A good way to develop code is to start with a blank file and write comments that describe how you are going to do it (A methodolgy or recipe). Then write the code, that does whats written in the comment, after the comment. Leave the comments in the code as documentation.

This help you organise all your thoughts together logically, rather tearing into writing code , then wondering why it doesn't work. It is a very basic way of "Designing" the code. Designing code is a concept that most new programmers seem to miss altogether.

Once you have written up the design as comments, you could look at the reference info on this site for all the things you need to use.

Cheers TheIdeasMan
I'm not sure if this is too advanced, but I understood it after a day or two.

Instead of having two different kind of structs, why not just have one that contains all the information? You can read from one file and fill out the struct with all the information on each employee.

http://www.cplusplus.com/reference/stl/map/
You would store each struct inside a map, using their employee ID as a key value. That would make it easy to search for the struct and fill out the rest of the information when you open the second file.
@fg109

Using maps is the C++ way, for sure, nothing wrong with that. However I notice that a lot guys in this beginners forum tend to write C code, with the only C++ aspects being cout, cin, string. This is probably because they are new to programming and it is neccessary to learn the basics of C before progressing on to C++. Often schools get the students to use C++ software, but the code is mainly C. The purpose is to keep things relatively simple.

These comments are a generalisation - it's not true for all the guys here. I am not saying that guys shouldn't take advantage of C++ features, but often that involves a whole lot of complexity and the person concerned might miss out on the concepts of the underlying data structures.

An example might be that we could get someone to use an STL object type like <list> and happily use all it's features. However the would miss out on understanding arrays. Schools often set assignments to implement some of the features of say <list>, by using arrays, or pointers to do say a double linked list with functions like insert and delete, so that the students really understand what is happening when they use the STL <list>.

When I say that C++ can introduce a lot of complexity - it's not always true, but often is. Take classes for example, it seems simple enough - a class has methods & properties. However it's not long before you hit constructors, destructors, polymorphism, inheritance etc. All that might be too much for only needing a program that deals with arrays. And they don't learn about arrays.

I hope I haven't started a war here :)

Cheers TheIdeasMan
I have the same problem to do, and while it compiles and runs, I have two issues. The first is it only gives me accurate pay for the first and last person. The second is all the text shows up on one line, instead of a space between each.

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
//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;
	
	
	eds >> name >> id1 >> rate;
	hrds >> id2 >> hours;
	while (!(eds.eof()))
	{
		pay = rate * hours;
			
	if (id1 == id2)
	{
		pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
		eds >> name >> id1 >> rate;
	}
	else if (id1 != id2)
	{
		hrds >> id2 >> hours;
		while (id1 != id2)
		{
			if (!(hrds.eof()))
			{
				hrds >> id2 >> hours;
			}
			else
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << noHours << endl;
				eds >> name >> id1 >> rate;
			}
		}
	}
	
	}
}
As I said in your thread, you're going to have to use seekg(). That's the reason for your first problem.

Think about it, your two input files are not organized the same way. Line X in one file is not referring to the same employee as Line X in the other file. At some point, this is going to happen:

You're reading employee.txt and find employee X. You read hours.txt and you find employees Y, X, Z. Since Y is not X, you skip Y and find X. You do your calculations and then output the info for X. The next line in employee.txt is for employee Y. But you're already on employee Z in hours.txt so you can't find the info for employee Y.

With your current code, you'll think that employee Y didn't work at all, when in fact he/she did. Since the lists aren't in the same order, you'll hit the end of file in hours.txt without processing all the info in it.

What you need to do is use seekg() to go back to the beginning of the file and read from there once you've hit the EOF. And you should do that when searching for hours of each employee. Only if you've hit EOF twice for the same employee should you decide that the employee did no work at all.

I have no idea why your stuff is all printing to the same line though.
Okay, I tried (I think) to implement what you've said. Below is what I have, but I'm stuck in a loop, as the first poster was.

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
113
114
#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 flag;
	flag = 0;
	int noHours = 0;
	
	
	eds >> name >> id1 >> rate;
	while (!(eds.eof()))
	{
		pay = rate * hours;
		hrds >> id2 >> hours;
			
	if (id1 == id2)
	{
		pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
		hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  	hrds.clear();  
		eds >> name >> id1 >> rate;
		  
	}
	else if (id1 != id2)
	{
		while ((id1 !=id2) && (!(hrds.eof())) && (flag != 1))
		{
			hrds >> id2 >> hours;
			if (id1 == id2)
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
				hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  			hrds.clear();  
				eds >> name >> id1 >> rate;
			}
			else if ((id1 != id2) && (hrds.eof()))
			{
				hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  			hrds.clear(); 
				flag = 1; 
			}
			else
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << noHours << endl;
				eds >> name >> id1 >> rate;
			}
			
		}
	}
	}
	flag = 0;
}
I see two problems with your nested loop. What happens if id1 != id2, but you haven't hit EOF yet?

Also, there's something wrong with the flags you set up. What happens if you really find an employee that didn't work? Think about where you are setting the flag to 1, and setting it back to 0.

Anyway, it would be a lot simpler (though maybe less efficient) if you went back to the code you had previously, and just add

hrds.seekg (0, ios::beg);

in the beginning of your outer while loop.

EDIT: Looks like I am remembering incorrectly what you had before. Anyway, this is what I mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
id2 = 0;
eds >> name >> id1 >> rate;
while (!eds.eof())
{
	hrds.clear()
	hrds.seekg (0, ios::beg);
	while (!hrds.eof() && id1 != id2)
	{
		hrds >> id2 >> hours;
	}
	if (id1 == id2)
	{
		pds << name << " " << id1 << " " << rate << " " << hours << " $" << rate * hours << endl;
	}
	else
	{
		pds << name << " " << id1 << " " << rate << " 0 $0.00" << end1;
	}
	eds >> name >> id1 >> rate;
}
Last edited on
I think I see what you're saying. I added the flags to try to get it to run twice and catch any strays. I feel like I'm getting closer, but I'm still getting a lot of zeros for the pay, and everything is still on one 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#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 flag;
	flag = 0;
	int noHours = 0;
	
	
	eds >> name >> id1 >> rate;
	while (!(eds.eof()))
	{
		pay = rate * hours;
		hrds >> id2 >> hours;
			
	if (id1 == id2)
	{
		pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
		eds >> name >> id1 >> rate;
		hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  	hrds.clear();  
		  
	}
	else if ((id1 != id2) && (hrds.eof()) && (flag = 2))
	{
		pds << name << " " << id1 << " " << rate << " " << hours << " $" << noHours << endl;
		eds >> name >> id1 >> rate;
		hrds.seekg (0, ios::beg); 		// rewind back to start of file
		hrds.clear(); 
		flag=0;
	}
	else if ((id1 != id2) && (flag !=2))
	{
		while ((id1 !=id2) && (!(hrds.eof())))
		{
			hrds >> id2 >> hours;
			if (id1 == id2)
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
				eds >> name >> id1 >> rate;
				hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  			hrds.clear();  
			}
			else if ((id1 != id2) && (hrds.eof()) && (flag < 2))
			{
				hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  			hrds.clear(); 
				flag++;
			}
			else if (id1 != id2)
			{
				hrds >> id2 >> hours;
			}
		}
	}
	
	}
}
You are making this so much more complicated than it has to be. I'm not sure how to tell you to improve it other than to completely re-write it for you.

The code I posted earlier wasn't bad, and would get the job done.
My apologies, your earlier code didn't pop up for me, I just now saw it. I was trying to use your advice without seeing an example. I have tried the mapping way and gotten it to work, it's just too advanced, I'm attempting to do it within the means of which I have learned.
Your code is fantastic, it works perfectly, all I have to figure out now is how to get things on separate lines so I can read it more easily.
Thanks to your help, I ended up with 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#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 "paySimpleOut.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
	double rate;		//input: employee pay rate from inFileEmp
	int id2;		//input: employee id from inFileHour
	int hours;		//input: employee hours worked from inFileHour
	double pay;		//output: pay
	int noHours;
	noHours = 0;

	
	
	id2 = 0;
	eds >> name >> id1 >> rate;
	while (!eds.eof())
	{
		hrds.clear();
		hrds.seekg (0, ios::beg);
		while (!hrds.eof() && id1 != id2)
		{
			hrds >> id2 >> hours;
		}
			if (id1 == id2)
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << rate * hours << endl;
			}
			else
			{
				pds << name << " " << id1 << " " << rate << " " << noHours << " $" << rate * noHours << endl;
			}
		eds >> name >> id1 >> rate;
	}
}


The only thing that doesn't seem right is that it's one continuous line. It does have what I believe is a break char though. Any tips? Also, in case you were curious, I ended up with this version from help from another forum.

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

using namespace std;

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

//Functions used
void processHours(std::istream&, std::map<int, double>&);	//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
	int id;
	double rate;
	double hours;
	string name;

	//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
	}
	
	std::map<int, double> aIdToHours;
   	processHours(hrds, aIdToHours);

	
    	while(eds >> name >> id >> rate)
    	{
     		hours = aIdToHours[id];
     	 	pds << name << " " << id << " " << rate << " " << hours << " $" << hours*rate << std::endl;
    	}

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



void processHours(std::istream& hrds, std::map<int, double>& oData)
{
 	 int id;
 	 double hours;
 	 while(hrds >> id >> hours)
  	{
   		 oData[id] = hours;
	}
}
The other solution is sort of what I said in my first post in this thread; use a map and read one file at a time.

I don't know why endl is not working for you. Maybe you should just use "\n" instead.
Last edited on
\n gives me an undeclared variable message when I try to compile. Maybe I'm using it wrong.

I've tried
 
pds << name << " " << id << " " << rate << " " << hours << " $" << hours*rate << \n;


and
 
pds << name << " " << id << " " << rate << " " << hours << " $" << hours*rate\n;


 
pds << name << " " << id << " " << rate << " " << hours << " $" << hours*rate << "\n";
Last edited on
Yes, you are using it wrong.
Use "\n" instead of \n.
It was because I was opening it in notepad instead of wordpad. Thank you for all the help.
Topic archived. No new replies allowed.