Sorry about a n00b question but I'm having trouble. I'm messing around with linked list to try to get a good handle on them for the program I have to write. The program below was intended to fill a link list with structures then print out each structure. Instead it is only printing out the last structures. If someone could tell me why this is clearly and concisely in words I understand that would be great =]
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
struct node
{
char name[20]; // Name of up to 20 letters
int age; // D.O.B. would be better
float height; // In metres
node *nxt; // Pointer to next node
};
node *start_ptr = NULL;
node *temp;
temp = new node;
fin.open ("tfile.data");
while (fin >> temp -> name)
{
fin >> temp -> age;
fin >> temp -> height;
temp -> nxt = NULL;
}
if (start_ptr == NULL)
start_ptr = temp;
while (temp != NULL)
{
cout << temp -> name<<" ";
cout << temp -> age<<" ";
cout << temp -> height<<" ";
temp = temp->nxt;
cout << endl;
}
return 0;
}