Click here to Skip to main content
15,845,785 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I'm trying to create a linked list which contains more than just a single piece of data. The program will run and allow me to enter as many structs as I want but when I quit it, it will correctly print out the last structure entered, twice, and then crash.

I assume I'm doing something not right with the pointers.

Here's the code:

void main(void)
{
	int x;
	struct Student
	{
		int id;
		string name;
		string courseCode;
		struct Student *next; 
	} *start, *p;

	Student student;
	start = NULL; 
	
	printf("Enter a sequence of integers, letter to end: ");
	while(cin>>x)
	{
		p = &student;
		start = (Student *)malloc(sizeof(struct Student));
		if(start == NULL)
		{
			cout<<"Not enough memory";
			exit(1);
		}
		cout<<"Enter Student ID: ";
		cin>>student.id;
		cin.sync();
		cout<<endl<<"Enter Student Name: ";
		getline(cin, student.name);
		cin.sync();
		cout<<endl<<"Enter Course Code: ";
		getline(cin, student.courseCode);
		cin.sync();
		
		student.next = p;

	}

	cout<<endl<<"In reverse order, the following details were entered: "<<endl;
	

	for(p = start; p != NULL; p = p->next)
	{
		cout<<student.id<<endl;
		cout<<student.name<<endl;
		cout<<student.courseCode<<endl;
	}
}
Posted

1 solution

In the last loop you're printing the data for student instead of for p.
student doesn't change in the loop, it's p that changes so you should be printing that instead.

Also, in C++ you really don't want to call malloc when you have new, these links will point you in the right direction;

http://msdn.microsoft.com/en-us/library/kewsb8ba(v=vs.71).aspx[^]
http://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c[^]


This slightly changed implementation kind-of does what you want I think;
C++
#include<iostream>
#include<string>

using namespace std;

struct Student
{
    int id;
    string name;
    string courseCode;
    struct Student* next;
};


void main(void)
{
    int x;

    Student* previous = NULL;
    Student* start = NULL;

    printf("Enter a sequence of integers, letter to end: ");
    while(cin>>x)
    {
        Student* student = new Student;
        student->next = NULL;

        if (start == NULL)
            start = student;

        cout<<"Enter Student ID: ";
        cin>>student->id;
        cin.sync();
        cout<<endl<<"Enter Student Name: ";
        getline(cin, student->name);
        cin.sync();
        cout<<endl<<"Enter Course Code: ";
        getline(cin, student->courseCode);
        cin.sync();

        if (previous != NULL)
            previous->next = student;

        previous = student;
    }

    cout<<endl<<"In reverse order, the following details were entered: "<<endl;


    for(Student* p = start; p != NULL; p = p->next)
    {
        cout<<p->id<<endl;
        cout<<p->name<<endl;
        cout<<p->courseCode<<endl;
    }
}



Hope this helps,
Fredrik
 
Share this answer
 
v2
Comments
BrianHamilton 16-Feb-13 16:08pm    
Works perfectly, thanks a million. Brian.
CPallini 16-Feb-13 16:13pm    
Then give him a 5. :-)
Fredrik Bornander 16-Feb-13 16:49pm    
Glad I could help.
Sergey Alexandrovich Kryukov 16-Feb-13 19:39pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900