Click here to Skip to main content
Rate this: bad
good
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 at 16-Feb-13 16:08pm
   
Works perfectly, thanks a million. Brian.
CPallini at 16-Feb-13 16:13pm
   
Then give him a 5. :-)
Fredrik Bornander at 16-Feb-13 16:49pm
   
Glad I could help.
Sergey Alexandrovich Kryukov at 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
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 424
1 Sergey Alexandrovich Kryukov 336
2 Shai Vashdi 318
3 OriginalGriff 265
4 Abhinav S 150
0 Sergey Alexandrovich Kryukov 9,169
1 OriginalGriff 5,290
2 Peter Leow 4,020
3 Maciej Los 3,535
4 Abhinav S 3,263


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