Trying to compare two files, each with two datasets, line by line to look for similarities in one dataset [Homework]

I have two files, HourlyRate.txt and HoursWorked.txt each file has an employee ID number, and either their hourly pay rate or how many hours they worked that pay period as applicable. Both files don't necessarily have all the same ID numbers. I need to check the ID numbers on one file, compare it to all the ID numbers on the other file, then process the results into overall pay (which I should be able to do easily, if I can get the first part down).

Files will be formated like this

123 14.56
231 21.85
538 5.95
etc

Where the first number is the ID and the second number is the pay rate or hours worked.

I've deleted my code and rewrote it several times. I can easily have the files read into a single long list, the trouble is getting file A to compare the first line to all the lines on file B, and then move on to the next line and repeat the process if nothing is found. Here's where I left off after my last attempt.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
int         ID1,
            ID2,
            Hours,
            totalPay;
float       Rate;
ifstream    hourlyRate,
            hoursWorked;
string      string1,
            string2;


    hourlyRate.open("filepath\\HourlyRate.txt");
    hoursWorked.open("filepath\\HoursWorked.txt");
    while (getline (hourlyRate, string1));
    {
        cout << string1 << endl;
    }
return 0;
}

So far, I've tried going at it with getlines (which you can see above), with (hourlyRate << ID1 << Rate), and a few other things that were even less effective. I would try arrays, but I don't understand them at all. Not looking for a handout here, but I've been at this for a good six hours and have failed to make any real progress. If someone could point me in the direction on how to hold the ID number from one file and compare it to the ID numbers in the second file, then export the contents of those lines from both files, it would go a long way in helping me solve this issue.

As a side note, I know my code doesn't look like much, but my instructor said we should try to keep it to 15 lines or less.

Thanks in advance!
Last edited on
You can solve that with stringstream:

http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

To keep it short, like so:
1
2
3
    while (getline (hourlyRate, string1));
    {
        stringstream(string1) >> ID1;


Make a nested/inner loop that looks almost identical (1 -> 2). Then you can compare ID1 and ID2 and do the further steps if identical.
Topic archived. No new replies allowed.