I have a problem that I need some help with. I am trying to create a program that will create an intial list of students and grades, and that will allow me to modifyu,delete, and display the linked list. I have created the following code, which ruins with no errors, but it does not give me the intial list of students. any advice???
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct studentInfo {
char name[32];
int grade;
studentInfo * next;
};
studentInfo * student_list;
void printStudent(studentInfo * stud)
{
printf("Student: %s\n", stud->name);
printf("\tgrade: %d\n", stud->grade);
}
void printAllStudents(void)
{
struct studentInfo * current = student_list;
if (current != 0) {
while (current != 0) {
printStudent(current);
current = current->next;
}
} else {
printf("Error: No students have been entered yet!\n");
}
}
void addStudent(void)
{
printf("Creating a new student...\n");
studentInfo * temp = (struct studentInfo*) malloc(sizeof(struct studentInfo));
printf("Enter student name:");
gets(temp->name);
printf("Enter the student's grade:");
char buffer[8];
gets(buffer);
printf("Adding student %s to the list...", temp->name);
temp->next = student_list;
student_list = temp;
printf("Done!\n");
}
void getStudentInfo(void)
{
printf("Enter the name of a student:");
char search[32];
int found = 0;
gets(search);
printf("Searching the list...");
struct studentInfo * current = student_list;
while ((current != 0) && !found) {
if (!strcmp(current->name, search)) {
found = 1;
} else {
current = current->next;
}
}
printf("Done!\n");
if (current != 0) {
printStudent(current);
} else {
printf("No student by that name exists in the database\n");
}
}
void main(void) {
char current = 0;
student_list = 0;
printf("Welcome to Theresa's student manager.\n");
while (current != 'Q') {
printf("Enter a choice:\n");
printf("[V]iew all students\n");
printf("[A]dd a student\n");
printf("[G]et info on a student.\n");
printf("[Q]uit.\n");
current = getc(stdin);
/* Need this to pull off the return */
getc(stdin);
switch (current) {
case 'V':
printAllStudents();
break;
case 'A':
addStudent();
break;
case 'G':
getStudentInfo();
break;
case 'Q':
printf("See ya later...\n");
getc(stdin);
break;
default:
printf("Error: Unkown command!\n");
}
}
/* Free up all memory we've used */
struct studentInfo * temp = student_list->next;
while (student_list != 0) {
free(student_list);
student_list = temp;
temp = temp->next;
}
}