Reputation: 2580
I'm encountering a problem with displaying data from the linked list. I have tried to both include the display loop inside my for loop and out just to check whether it was a problem with pointers and data but I've been getting the same result.
It displays the first data but then starts showing gibberish.
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
void main(void) {
clrscr();
struct Student {
string Name;
double GPA;
Student *next;
};
Student *head;
head = NULL;
int ch, i;
string name;
double gpa;
cout << "How Many Records Do You Want To Enter?";
cin >> ch;
cout << endl;
for (i = 0; i < ch; i++) {
cout << (i + 1) << ". Name Of Student:";
cin >> name;
cout << "GPA Of Student:";
cin >> gpa;
cout << endl;
Student *newstudent;
Student *studentptr;
newstudent = new Student;
newstudent->Name = name;
newstudent->GPA = gpa;
newstudent->next = NULL;
if (!head)
head = newstudent;
else {
studentptr = head;
while (studentptr->next) {
studentptr = studentptr->next;
}
studentptr->next = new Student;
}
}
clrscr();
Student *display;
display = head;
while (display) {
cout << "Name:" << display->Name << endl;
cout << "GPA:" << display->GPA << endl;
display = display->next;
}
getch();
}
Any suggestions and pointers towards the right direction?
Apparently I was following someone's tutorial but this error occurs.
Upvotes: 2
Views: 5904
Reputation: 7249
I have a few suggestions that may help:
struct Student {
string Name;
double GPA;
Student *next;
Student(const string& name, double GPA) : Name(name), GPA(GPA), next(NULL) {}
void print() {
cout << "Name:" << Name << endl;
cout << "GPA:" << GPA << endl;
}
};
now instead of:
newstudent = new Student;
newstudent->Name = name;
newstudent->GPA = gpa;
newstudent->next = NULL;
You simply write:
newstudent = new Student(name, gpa);
Make a struct for the list:
struct StudentList {
Student* head;
Student* current;
StudentList() :head(NULL), current(NULL) {}
~StudentList() {/*Delete list here*/}
void insert(string name, double gpa) {
if(!head) {
head = new Student(name, gpa);
current = head;
} else {
current->next = new Student(name, gpa);
current = current->next;
}
}
void display() {
Student *display = head;
while (display) {
display->print();
display = display->next;
}
}
};
With all this your main should now be:
int main(void) {
clrscr();
StudentList list;
int ch, i;
string name;
double gpa;
cout << "How Many Records Do You Want To Enter?";
cin >> ch;
cout << endl;
for (i = 0; i < ch; i++) {
cout << (i + 1) << ". Name Of Student:";
cin >> name;
cout << "GPA Of Student:";
cin >> gpa;
cout << endl;
list.insert(name, gpa);
}
clrscr();
list.display();
getch();
}
Upvotes: 1
Reputation: 153802
Since the question is about suggestions in the right direction:
if (std::cin >> value) { ... }
.std::endl
.Student
object in your loop.Student
object with the list. You probably meant to do this where you create a new object instead.Upvotes: 2
Reputation: 4752
studentptr->next = new Student;
should be studentptr->next = newstudent;
Upvotes: 2