-2

There are two files, each has multiple columns of data, up to around 14,000 rows, neatly spaced and everything. File1 has 6 columns (Student ID #, semester code #, class name, class code # (though some have letters), the letter grade the student received, and the numeric grade they received.

The second file has 4 columns. Class name, class code, how many hours per week it is, and designation code (three letters indicating whether its a liberal arts class or not).

The task is to output everything from the first file into the new file, but add on two columns (from the second file) corresponding to each appropriate row, that have the hours for the course and designation code.

The second task is to take this new file, and output into it the students ID, overall GPA, GPA in CSCI courses, and a percent of hours spent taking non-liberal arts courses.

I'm not asking for someone to do it for me (obviously), it's just that I've run out of ideas. We're supposed to use nothing more than fstream, iostream, strings, if statements, loops, functions, and " .clear(); " and " seekg(ios::beg); " (also we're not supposed to use getline) basically super simple stuff, no arrays or vectors or anything.

I figured out how to output parts of the two files into the third file using while loops and if statements, but I have no idea how to tell it to compare values in a column from one file to a column in a different file and that if the values are equal, to output the corresponding values from the other columns (the amount of hours for each class and designation code). And I need a lot of help with the second task as well.

1

1 Answer 1

0

What you're looking for is a map. If you need help streaming into a map you can check out this post: Is there a Way to Stream in a Map?

But what you'll want to do is stream File2 into a map, useing the "class code" as the map key, and a tuple or your own custom struct as value. Then index that map with the "class code #" from the line you're currently outputting from File1, appending the appropriate elements of the map's indexed value.

All this may sound like hand-waving, so, because the question lacks an exemplary input and output, I have created an exemplary File1 input as though it had already been streamed in: tuple<int, int, string, string, char, int> File1[] = {make_tuple(13, 1, "Computer Science 1", "CS101", 'A', 100), make_tuple(13, 2, "Computer Science 2", "CS201", 'A', 100)}; and File2 input as though it had already been streamed in: map<string, tuple<string, int, string>> File2 = {make_pair("CS101", make_tuple("Computer Science 1", 4, "NOT")), make_pair("CS201", make_tuple("Computer Science 2", 4, "NOT"))};

These can then be streamed out, potentially to another file as follows:

for(auto& it : File1) {
    const auto& i = File2[get<3>(it)];

    cout << get<0>(it) << ' ' << get<1>(it) << ' ' << get<2>(it) << ' ' << get<3>(it) << ' ' << get<4>(it) << ' ' << get<5>(it) << ' ' << get<1>(i) << ' ' << get<2>(i) << endl;
}

[Live Example]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.