please help

I cant figure out why my program goes into an infinite loop when i exit my entries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// LinkedListData - store data in a linked list of objects
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

//NameDataSet - stores a person's name (these objects
//              could easily store any other information desired)

class NameDataSet
{
    public:
        string sName;
    // the link to the next entry in the list
    NameDataSet* pNext;
};

// the pointer to the first entry in the list
NameDataSet* pHead=0;

// add - add a new member to the linked list
void add(NameDataSet* pNDS)
{
    //point the current entry to the beginning of the list
    pNDS->pNext = pHead;

    //point the head pointer to the current entry
    pHead = pNDS;
}

//getData - read a name and social security number; return null if no more to read
NameDataSet* getData()
{
    //read the first name
    string name;
    cout<< "\n enter name: ";
    cin >> name;

    // if the entered is exit...
    if(name == "exit")
    {
        //...return a null to terminate
        return 0;
    }

    // get new entries and fill in values
    NameDataSet* pNDS = new NameDataSet;
    pNDS->sName = name;
    pNDS->pNext = 0; //zero link

    // return the address of the object created
    return pNDS;
}

int main()
{
    cout << "Read names of students\n"
         << "Enter 'exit' for first name to exit"
         << endl;

        // create (another) NameDataSet object
        NameDataSet* pNDS;

    while (pNDS = getData())
    {
        // add it to the list of NameDataSet objects
        add(pNDS);
    }

    // to display the objects, iterate through the
    // list (stop when the next address is NULL)

    cout << "\nEntries: "<<endl;
    for(NameDataSet *pIter = pHead; pIter; pIter -> pNext)
    {
        // display name of current entry
        cout << pIter->sName << endl;
    }
    cout <<"\n\n";

    system("PAUSE");
    return 0;
}
for(NameDataSet *pIter = pHead; pIter; pIter -> pNext)
Did you mean pIter = pIter -> pNext?
Topic archived. No new replies allowed.