Click here to Skip to main content
12,294,380 members (67,428 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C
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 16-Feb-13 8:21am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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;
#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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100