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