-1

Can anyone help me in this code? I am trying to read data from file into a linked list . My linked list is something like this:

The data in the file are in the format like this:

 ####****###

**####*****#

and so on

           node *copy = new node();
           rownum=1;
           seatnum=1;

           while(!fin.eof())
           {
            fin>>copy->data;
            cout<<copy->data;
            copy->next=NULL;

            if(head==NULL)
            {
                copy->next=head;
                head=copy;
            }
            cout<<"Row number-" <<copy->row<<"    "<<copy->data;
            copy->row=rownum;
            copy->seat=seatnum;
            seatnum=seatnum+1;
            if (seatnum==total)// total is total number of seats in a row
            {
                seatnum=1;
                rownum=rownum+1;
            }


    }
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • 3
    Please read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Also please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). Finally [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Mar 21 '17 at 12:19
  • You don't tell us what is wrong and you are missing things like what `node` is and where `head` is declared and if it is initialized. – crashmstr Mar 21 '17 at 12:23
  • struct node{ char tdata; int row=1; int seat=1; node* next; node* prev; }; this is my node and i have declared head globally. – Abinash Bhattarai Mar 21 '17 at 12:30
  • All I have to do is read from a file as in the above format and store it in the linked list and modify the linked list and store back in the file again. And I am still not able to read from file into linked list. :( – Abinash Bhattarai Mar 21 '17 at 12:38
  • There are two things that are problematic: The first is that a list should be a list of *different* nodes. You don't create new nodes as you read. The second problem is that if `head` is not `NULL` then you don't add any nodes to the list. – Some programmer dude Mar 21 '17 at 12:42
  • How do I fix it up? I am new to linked list. And I am so much having problem working on it. – Abinash Bhattarai Mar 21 '17 at 12:49

1 Answers1

0

In each time in loop You must have to create a new linked list

node *copy = new node();
       rownum=1;
       seatnum=1;

       while(!fin.eof())
       {
         node *tmp= new node();
        fin>>tmp->data;
        cout<<tmp->data;
        tmp->next=NULL;

        if(head==NULL)
        {
            tmp->next=head;
            head=tmp;
        }
        cout<<"Row number-" <<tmp->row<<"    "<<copy->data;
        tmp->row=rownum;
        tmp->seat=seatnum;
        copy->next = tmp;
        seatnum=seatnum+1;
        if (seatnum==total)// total is total number of seats in a row
        {
            seatnum=1;
            rownum=rownum+1;
        }


}
Mithilesh
  • 155
  • 1
  • 8