GuestUser140561
GuestUser140561

Reputation: 67

Infinite loop when loading Linked List from Text File and printing

I'm having trouble reading from a file into a linked list. I have a (probably extremely inefficient way) of reading the file and loading each node into the linked list, however if I read and attempt to print more than 1 line then I get an infinite loop when printing.

Load Code

void readFile()
{
    string text;
    string temp; // Added this line
    node* newNode = new node;

    ifstream file;
    file.open("example.txt");

    for (int i = 0; i < 1; i++)
    {
        getline(file, temp);
        text = temp;

        string input = text;
        istringstream ss(input);
        string token;

        int counter = 0;

        while (getline(ss, token, ','))
        {
            cout << token << '\n';
            newNode->rented = token;
            counter++;

            if (counter == 0)
            {
                newNode->rented = token;
            }
            else if (counter == 1)
            {
                std::istringstream ss(token);
                ss >> newNode->maxload;
            }
            else if (counter == 2)
            {
                std::istringstream ss(token);
                ss >> newNode->passengers;
            }
            else if (counter == 3)
            {
                std::istringstream ss(token);
                ss >> newNode->doors;
            }
            else if (counter == 4)
            {
                newNode->registration = token;
            }
            else if (counter == 5)
            {
                std::istringstream ss(token);
                ss >> newNode->engine;
            }
            else if (counter == 6)
            {
                newNode->model = token;
            }
            else if (counter == 7)
            {
                newNode->make = token;
            }
        }
        system("pause");
        list.insertNode(newNode);
    }
    file.close();
}

Insert Node

void linkedList::insertNode(node* newNode)
{
    newNode->nextNode = head;
    head = newNode;
}

Print Code

void linkedList::displayList()
{

    node* thisNode = head;

    if (head == NULL)
    {
        cout << "The list is empty\n";
        return;
    }
    else
        cout << "---------------------------------------------------------\n";
        cout << "\tMake\tReg Number\tRented\n";
        cout << "---------------------------------------------------------\n";
        cout << "\t";


    do
    {
        cout << setw(8) << left << thisNode->make;
        cout << setw(16) << left << thisNode->registration;
        cout << setw(10) << left << thisNode->rented;
        cout << "\n\t";
        thisNode = thisNode->nextNode;

    } while (thisNode != NULL);
    {
        cout << "\n\n";
    }
}

Text File

car,Ferarri,12.0,aa,3,1,0,true
car,Mercedes,12.0,bb,5,4,0,false
car,Ford,1.6,cc,5,4,0,false

If I set the for loop in the load code to just 1 iteration it outputs the correct display of:

0 aa true

However if I set the for loop to iterate more than 1 time the print code will infinitely print whatever the last line read was (ignoring all lines before it)?

0 bb false
0 bb false
0 bb false
...

Can anyone see any reason why this is doing this?

Upvotes: 0

Views: 113

Answers (1)

PaulR
PaulR

Reputation: 3707

You are just allocating one node. Move the following line to the start of the for loop:

node* newNode = new node;

Otherwise you overwrite everything in the node everytime and then add it repeatedly to your list, creating a circular reference.

Upvotes: 2

Related Questions