hello, can someone help me, when I put the codes from my book and edited them to use filestream, I encounter an error

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct nodeType
{
    string info;
    nodeType *link;
};
int main()
{
    nodeType *head = NULL;
    nodeType *newNode;
    nodeType *p;
    ifstream reeaad;
    reeaad.open("meow.tx");
    newNode = new nodeType;
    while(!EOF)
    {
        getline(reeaad,newNode->info);
        newNode->link = NULL;
    };
    p=head;
    p->link=newNode;
    nodeType *temp;
    int counter = 1;
    temp = head;
    while(temp!=NULL)
    {
        cout<<"node:"<<counter<<" ";
        cout<<"Info:"<<temp->info<<"\n";
        cout<<"---------------\n";
        temp = temp ->link;
        ++counter;
    };
    system("pause");
    return 0;
};
nodeType *head = NULL;
...
p=head;
p->link=newNode;

head is NULL, so p is NULL, so p->link does not exist.

Edited 4 Years Ago by Moschops

I tried removing the

p = head;
    p->link = newNode;

but it did not print anything

So you removed those lines, and then

temp = head;

so now temp is NULL as well, and then

while(temp!=NULL)

but you just set temp to NULL, so the while loop never executes, and the program finishes.

You need to stop guessing and start thinking. What is head supposed to be?

If you don't understand what a pointer is, read up on them first. Here is what I usually say about pointers, permanently written out so I don't have to type it again:
http://www.cplusplus.com/articles/EN3hAqkS/

Edited 4 Years Ago by Moschops

Rather than eliminating code, in this case you need to add a line just above the first while() statement (line 20):

head = newNode;

This sets the head of the list, which you'll need in order to access the list as it progesses.

There's a problem within the while() loop itself, as well; rather than setting newNode->link to NULL, you need to set it to a new node, then progress newNode itself.

    newNode = new nodeType;
    head = newNode;  // initialize the handle for the list
    while(!EOF)
    {
        getline(reeaad,newNode->info);
        newNode->link = new nodeType;
        newNode = newNode->link;
    }

Otherwise, you are simply overwriting the data as it comes in.

This question has already been answered. Start a new discussion instead.