Problem with program not working.

Hello,
I have a problem with a program. I'm using Borland C++ ver. 4.52.
It's on a PC that's 200 Mhz. I put the following code in, and it
works for a while - and then it stops working. Simply copying
to another name will cause is the stop, and then the original
one stops too.
Here's the path:
PATH=C:\BC45\BIN;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\BC45;
C:\BC45\JIM\CLOCK;C:\BC45\JIM
I type ver and get:
Windows 95. [Version 4.00.950]
The filename is jlink2.cc
I'm sorry, can you help please?
Jim Devaney


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

// NameDataSet - stores a person's name (these objects
// could easily store any other information
// desired.
class NameDataSet
{
public:
char szName[128];

// 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
char nameBuffer[128];
printf ("\nEnter name:");
gets (nameBuffer);

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

// get a new entry to fill
NameDataSet* pNDS = new NameDataSet;

// fill in the name and zero the link pointer
strncpy(pNDS->szName, nameBuffer, 128);
pNDS->szName[127] = '\0';
pNDS->pNext = 0;

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

int main(int nNumberofArgs, char* pszArgs[])
{
printf ("Read names of students\n");
printf ("Enter 'exit' for first name to exit\n");

// create (another) NameDataSet object
NameDataSet* pNDS;
while (pNDS = getData())
{
// add it onto the end of the list of
// NameDataSet objects
add(pNDS);
}

// to display the objects, iterate through the
// list (stop when the next address is NULL)
printf ("Entries:\n");
pNDS = pHead;
while(pNDS)
{
// display current entry
printf (pNDS->szName);
// get the next entry
pNDS = pNDS->pNext;
}
getch();
return 0;
}
Please use the format tags when posting code.

The code looks fine to me.

Two things. There is there's no check for NameDataSet* pNDS = new NameDataSet; failing in getData(). The list is not cleaned up at the end of the run.

One minor thing, you should print an crlf after each name when displaying the list.
Topic archived. No new replies allowed.