0

This is my text file named read.txt:

ID Name
22 Joe
33 Jim
ID Name
44 Bob
55 Jeff
ID Name
66 Mike

Task: Every time the code encounters the string "ID", it has to add the line after it into a list. So, for example:

(List1)
22 Joe
33 Jim

(List2)
44 Bob
55 Jeff

(List3)
66 Mike

Problem: I'm having trouble with reading the text file. I looked up other questions that have been asked on here and saw that cin.fail() might be of use. So, I looked it up in my textbook and tried implementing it but the program just displays a blank screen when I compile. When I take out all the strings ID and just have the numbers and names, the code works perfectly.

This is the section of my code:

int main()
{
    ifstream file_read;
    file_read.open("read.txt");

    LinkedList list1, list2, list3, list4;  //lists are of type linkedlist
    string name, name2, name3;      
    int ID;                 
    int counter = 0;    //counter initialized
    while (file_read.is_open()) {   //run loop until end of file reached
        if (file_read.fail()) {      //when cin fails
            counter++;               //increment counter
            if (counter == 1) {       //if counter is 1
                file_read >> ID >> name;    
                list1.Add(name, ID);   //add following names to list1
            }
            if (counter == 2) {  //if counter value is 2 
                file_read >> ID >> name2;
                list2.Add(name2, ID);  //add to list 2
            }
            if (counter == 3) {    //if counter value is 3
                file_read >> ID >> name3;
                list3.Add(name3, ID);   //add to list 3
            }
        }
    }
    file_read.close();
}

List 1,2,3 are linkedlists. Add is a function of linked list. These parts of the code work correcly, I am only having trouble with the text-file portion

Output: I'm getting a blank screen.

Could someone tell me what I'm doing wrong and how I can split the text file data into sections whenever I encounter a string? Thank you in advance.

ss1111
  • 199
  • 6
  • 18
  • 3
    ([Don't use `while (!file_read.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)) – Biffen Apr 23 '16 at 15:31
  • 2
    `if (counter = 1) {` => `if (counter == 1) {` – πάντα ῥεῖ Apr 23 '16 at 15:33
  • 1
    `continue` does not do what you think it does. – Mat Apr 23 '16 at 15:33
  • 1
    Spend some time putting together a [MCVE] and you'll soon find out what the problems are. That's your job, not ours!! – Lightness Races in Orbit Apr 23 '16 at 15:34
  • 1
    Those `else continue;` lines are quite superfluous. `counter` will only have one value at a time (once the comparisons are fixed). – Biffen Apr 23 '16 at 15:34
  • 1
    @Biffen is completely right here. Conditioning only on eof is not idiomatic and completely ignores cases where the read could fail for any other reason. [Check out this awesome article on C++ IO streams](http://umich.edu/~eecs381/handouts/basicCppio.pdf) – Tanner Apr 23 '16 at 15:38
  • I removed the incorrect `else continue;` statements, fixed the `while` loop parameter. Is there still an error with this code? because I am still getting a blank output. – ss1111 Apr 23 '16 at 15:54
  • 1
    @ss1111 `=` vs `==` is a *huge* issue. – Biffen Apr 23 '16 at 15:59
  • @Biffen I was following the first comment because I thought `==` was used to check if the condition is true that they are equal. But now I am confused about whether to use `=` or `==`. – ss1111 Apr 23 '16 at 16:04
  • @ss1111 You want `==` in this case. May I suggest a reread of your favourite C++ learning source? – Biffen Apr 23 '16 at 16:38

1 Answers1

0

You probably mean

if (! file_read.fail()) {

instead of if (file_read.fail())? After all, you only want to add to the list if the read was successful.

Guido
  • 846
  • 5
  • 13