Help needed with C++ I have a text file named \"read.txt\" which has: Everytime
ID: 3693036 • Letter: H
Question
Help needed with C++
I have a text file named "read.txt" which has:
Everytime the code sees the string "ID", we need to make a new list with the lines after it until the code sees another string "ID". So, the output needs to look like this:
Here is my code so far:
LinkedList list1, list2, list3; //declare lists of type linked list
string name, name2, name3; //name variable for list 1 and list 2
int ID; //initialize RUID
int counter = 0;
while (file_read.is_open()) {
file_read >> ID >> name;
if (file_read.fail()) {
counter++;
if (counter = 1) {
file_read >> ID >> name;
list1.Add(name, ID);
}
if (counter = 2) {
file_read >> ID >> name2;
list2.Add(name2, ID);
}
if (counter = 3) {
file_read >> ID >> name3;
list3.Add(name3, ID);
}
}
}
file_read.close();
I just need help with the loop posted above. I need help with just splitting up the text file whenever a string "ID" is read and putting the lines after it into list1,list2,list3. All of those work correctly and they are functions in my code. The "Add" is also a function that works. I just need help with the loop. Thank you.
Explanation / Answer
Ans;
instead of if (file_read.fail())? After all, you only want to add to the list if the read was successful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.