I'm working on a project for school. It's relatively simple, at least it appears to be: take file one (named sd, or student data, in the nomenclature of my code below), which contains student ID data and compare it to file two (named hc, the name of my college). File two contains a list of all active courses at my college, and has additional information that is not contained in file one: the number of course hours for each class, and a special code that indicates what sort of course it is. The goal is to replicate all the data in the student ID file and append each line with the relevant course hours and special code. Here's an example below:
Sample of a line from file 1 (SD):
20424297 1142 PSYCH 22000 W -1
Sample of a line from file 2 (HC):
PSYCH 22000 3.0 RLA
The goal is to take the 6k lines from SD, match them with the course data from HC (around 13k lines), and output an additional 6k in a third file that looks like this:
20424297 1142 PSYCH 22000 W -1 3.0 RLA
Our course is a relatively introductory CS course, and we've been exposed to only a few basic concepts in CS and C++. This project is intended to be solved using only the very rudimentary ideas we've discussed (file streams, while loops, for loops, etc). So after much thought, I realized that a series of nested while loops ought to be able to resolve this. Have one while loop active while I stuff data from file 1's input stream into variables, and have another while loop active that will open up file two, searching through the entirety of that data, and output a line when a match is made. I know it's somewhat inefficient to have the entirety of file 2 opened up and searched through for each line of file one, but our professor explicitly stated that speed and efficiency are not the priorities for this assignment.
Here is the code I created below:
#include <fstream>
#include <iostream> // INCLUDED FOR TESTING ERROR STATEMENTS
using namespace std;
int main ()
{
ifstream sdStream;
ifstream hcStream;
ofstream outputStream;
sdStream.open ("StudentData.tsv"); // use this to take in data from SD file
hcStream.open ("HunterCourses.tsv"); // use this to open stream for HC file
outputStream.open("File1.tsv"); // this will be the csombine data file created by this output stream.
//Student Data String Objects
string eiD;
string semester;
string subject;
string coursenumSD;
string grade;
string gradeNum;
//Hunter College Data String Objects
string subjectHC;
string coursenumHC;
double gpaHC;
string codeHC;
// bool streamactive = sdStream >> eiD >> semester >> subject >> coursenumSD >> grade >> gradeNum;
// USE THE BELOW AS A STANDARD TRANSFER DATA TEMPLATE
// while (sdStream >> eiD >> semester >> subject >> coursenumSD >> grade >> gradeNum)
// {
// hcStream >> subjectHC >> coursenumHC >> gpaHC >> codeHC;
// outputStream << endl << eiD + ' ' << semester + ' ' << subject + ' ' << coursenumSD + ' ' << grade + ' ' << gradeNum + ' ';
// }
while (sdStream >> eiD >> semester >> subject >> coursenumSD >> grade >> gradeNum) // while stream from SD file is open, execute below
{
while (hcStream >> subjectHC >> coursenumHC >> gpaHC >> codeHC) // while stream from HC file is open, execute below
{
if ((coursenumHC == coursenumSD) && (subject == subjectHC)) // while identifying info from file 1 = date from file 2, out put the below
{
outputStream << endl << eiD + ' ' << semester + ' ' << subject + ' ' << coursenumSD + ' ' << grade + ' ' << gradeNum + ' ' << gpaHC + ' ' << codeHC + ' ';
}
}
}
outputStream.close(); sdStream.close(); hcStream.close();
return 0;
}
Hopefully this will not to be too onerous to read though. For readability's sake, here is the loop in question:
while (sdStream >> eiD >> semester >> subject >> coursenumSD >> grade >> gradeNum) // while stream from SD file is open, execute below
{
while (hcStream >> subjectHC >> coursenumHC >> gpaHC >> codeHC) // while stream from HC file is open, execute below
{
if ((coursenumHC == coursenumSD) && (subject == subjectHC)) // while identifying info from file 1 = date from file 2, out put the below
{
outputStream << endl << eiD + ' ' << semester + ' ' << subject + ' ' << coursenumSD + ' ' << grade + ' ' << gradeNum + ' ' << gpaHC + ' ' << codeHC + ' ';
}
}
}
The output of this is a single line, successfully matching the first line of file 1 (SD) with the pertinent data in file 2 (HC). And that's it. The loop ends. I showed this code to a few other folks who told me that the issue was that when HC's input stream opens up for the 2nd line of SD, it opens up where it left off. I'm not sure that's the case, but it seems plausible. Is there anyway I can ensure this loop cycles through from the beginning of file 2 (HC) to the end of file 2 for each line of file one? I've tried everything I can think of, from opening and closing the input stream for file 2 within the loops to experimenting with the .eof() command - though I'm not sure i'm using it at all correctly.