linked student list need help

This is a discussion on linked student list need help within the C Programming forums, part of the General Programming Boards category; I have a problem that I need some help with. I am trying to create a program that will create ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    linked student list need help

    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;
    	}
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Well, unfortunately, your addStudent function just isn't adding students.

    Also, use the first struct pointer to cycle thru your list.

    Good luck
    Bob

    Code:
    struct studentInfo {
        char name[32];
        int grade;
        studentInfo * next;
    }*first;
    
    void addStudent(void)
    {
        char buffer[8] = {0};
        struct studentInfo  *temp = NULL;
        struct studentInfo *current;
        printf("Creating a new student...\n");
        /* Allocate memory for the new structure that first->next will
           point too. Then clear it with memset to be certain we're NOT
           pointing to incorrect address.  It's better to be safe than sorry... 
         */
        temp= (struct studentInfo *)malloc(sizeof(struct studentInfo));
        memset(temp, 0, sizeof(struct studentInfo));
        /* We must allocate memory for first entry. */
        if(first == NULL) {
            first = (struct studentInfo *)malloc(sizeof(struct studentInfo));
            memset(first, 0, sizeof(struct studentInfo));
            current = first;
        } 
        else {
            current = first;
            /* Follow the links through the list until we hit the end */
            while(current->next != NULL)
                current = current->next;
        }
        current->next = temp;
        printf("Enter student name:");
        gets(current->name);
        printf("Enter the student's grade:");
        gets(buffer);
        current->grade = atoi(buffer);
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Are u compiling it as c++ code ?

    In ansi c :
    A. declaring arguments should be done before anything else.
    B global varibales static or external are guraranteed to be initalized to zero;

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In C99:

    A. declaring arguments should be done before anything else, because it looks like crap otherwise, and makes for ugly code, but it's not required.

    B. B is for Beer, it's good enough for me.

    C. Cookie, cookie, cookie starts with C!

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    void main
    gets(buffer)
    casting malloc....

    I'm gonna puke!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21