'C++ Is there any way to classify parts of a text file then use them seperately?
I am currently trying to do my college homework, we are studying file streams and vectors in C++ right now, our task is long but I will just try to explain my main problem:
We need to read multiple text files and get information and store them in vectors, anyways, in our first text file, there are parts we need to read like this:
HOMEWORK
00011234 84
00012341 90
00012481 100
MIDTERM
00011234 55
00012341 99
00012481 50
(student Id, grade)
etc... In our task, we need to get 10% of homework grade, 20% of midterm grade and 30% of final grade, and then take the sum of grades related with student id's.
My question is: is there any way to read by iteration or group these informations, and use them to take related percentages? (btw we didn't learn classes/structs/pointers yet, so it's forbidden to use them in our task, just vectors, fstream, sstream, string libraries.) Also, there are multiple homework grades for a student, or only 1 homework grade etc. (We need to take percentages and sum all grades of that student)
Solution 1:[1]
I don't know if there is a way to use getline function until a specific line.
There are many ways of going about that, but a simple one is to use the break keyword to exit out of a loop when a certain condition is met.
std::ifstream file_stream ("file.text");
std::string line_text;
while(getline(file_stream, line_text)) {
if(line_text == "MIDTERM") { // or whatever other test makes sense for your situation.
// This will make the program skip to immediately after the loop.
break;
}
// Handle otherwise
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
