Linked List Problems

I'm trying to write a program that has a user input student names, their ages, their GPAs, and their graduations dates (i.e. 'F13', F for Fall, S for Spring, then the last two digits of the year). This information goes into a linked list, then the user can search a name or part of a name to find students and display their information.

I am having a few problems, however. 1) when I use the displayStudentNames function, the names are being printed in reverse order from what I put in, 2) graduation dates aren't being printed out, so I'm not sure if they're being stored correctly, if at all, 3) when I use the displayStudentInfo function, it either works incorrectly, or crashes the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstring>
using namespace std;

const char NAME_SIZE = 50;

struct StudentInfo
{
   char studentName[NAME_SIZE];
   int age;
   double gpa;
   char graduationSemester[4];
   StudentInfo *next;
};

void displayStudentNames(StudentInfo *top);
void displayStudentInfo(StudentInfo *top);

int main(){
	StudentInfo *top = 0;
	cout << "Please enter the students. Enter the name, age, gpa, and semester of graduation (e.g. F13)." << endl;
	cout << "Enter an empty name to stop." << endl << endl;
	bool done = false;
	while(!done){
		char graduationBuffer[4];
		char nameBuffer[NAME_SIZE];
		cin.getline(nameBuffer, NAME_SIZE);
		if(nameBuffer[0] != 0){
			//create new struct in list
			StudentInfo *temp = new StudentInfo;
			strcpy(temp->studentName, nameBuffer);
			//do-while loop for age input & validation
			do{
				cin >> temp->age;
				if(temp->age < 1 || temp->age > 120){
					cout << "Please choose an age between 1 and 120." << endl;
				}
			}while(temp->age < 1 || temp->age > 120);
			//do-while loop for gpa input & validation
			do{
				cin >> temp->gpa;
				if(temp->gpa < 0 || temp->gpa > 4){
					cout << "Please choose a gpa between 0 and 4." << endl;
				}
			}while(temp->gpa < 0 || temp->gpa > 4);
				cin.getline(graduationBuffer, 4);
				strcpy(temp->graduationSemester, graduationBuffer);
			cin.ignore(80, '\n');
			//begins next node in list
			temp->next = top;
			top = temp;
		//else statement occurs when user input is ended by empty name
		}else{
			//function is called to display all input names
			displayStudentNames(top);
			//function is called to ask for name to search for
			displayStudentInfo(top);
			//after functions are called, boolean set to true to end the program
			done = true;
		}
	}
}

//function to display the names of students in the linked list
void displayStudentNames(StudentInfo *top){
	cout << "Here are the students that you entered: " << endl << endl;
	while(top){
		cout << top->studentName << endl;
		top = top->next;
	}
	cout << endl;
}

//function to search for student and display info
void displayStudentInfo(StudentInfo *top){
	char name[NAME_SIZE];
	StudentInfo *node = top;
	bool done = false;
	while(!done){
		cout << "Which student do you want? ";
		cin.getline(name, NAME_SIZE);
		if(name[0] != 0){
			do{
				const char *str = top->studentName;
				const char *substr = name;
				const char *index = str;
				if(node->studentName == strstr(node->studentName,name)){
					cout << "Name: " << top->studentName;
					cout << ", Age: " << top->age;
					cout << ", GPA: " << top->gpa;
					cout << ", Graduation Date: " << top->graduationSemester;
					cout << endl << endl;
				}
				node = node->next;
			}while(node != NULL);
		}else{
			cout << "No students found." << "Goodbye!" << endl;
			done = true;
		}
	}
}
Last edited on
Hi,

There are quite a few problems I can see:

The implementation of the container (linked list) should be separate from the data it stores. Normally there is a struct for the linked list, and one for a Node which holds a data item.

The linked list should have at least functions for push and pop and print. It is much more tidy and less error prone if you can call these functions. Also have a separate function to get and validate input. For example get all the the info for a student and validate it with a function, then push the whole thing onto the end of the list with one function call.

The linked list struct should also have a separate pointer for the head of the list. That way you can always start at the beginning of the list, and not with a moving target like top

You are leaking memory: using new without delete

Is there a reason why you are taking a C approach by having char arrays and using C functions like strcpy? Maybe your assignment requires it for some mad reason? Things are much easier if you can use std::string

1) when I use the displayStudentNames function, the names are being printed in reverse order from what I put in


Did you implement a stack rather than a list?

Any way see how you go :+) There are plenty of people to help, I am sure we all look forward to seeing your new code :+)
Topic archived. No new replies allowed.