program break help!!

This is a discussion on program break help!! within the C Programming forums, part of the General Programming Boards category; in the program i built from a "students.txt" file a binary tree. and from each students i make a new ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    Lightbulb program break help!!

    in the program i built from a "students.txt" file a binary tree. and from each students i make a new linked list the the grades that i took from "grades.txt" file.
    the code i put here is the code for the 2 functions that build the grades list from the "grades.txt" file, the programs runs but break in the while loop. i dont know why?? help please, thanks!! if you want to see the whole code you can enter: C | // -----------------------------------------------

    the students.txt file is build from:
    id number - 9 chars
    first name - 26 chars
    last name - 26 chars
    space between each field.

    the grades.txt file is build from:
    id number - 9 chars
    grade - int
    exercise - int
    space between each field.

    2 functions code, the second calls the first, the break happen in the second in the while loop:

    Code:
    // ----------------------------------------------------
    //	Synopsis: Recursively insert student grades to tree.
    //
    //	Arguments: 
    //		- Grades file name
    //		- pointer for tree root
    //
    //	Return-Value: FALSE on failure. TRUE otherwise 
    // ----------------------------------------------------
    int InsertGradesTreeNode(StudentNode * proot, GradeNode * newGrade, char id[ID_SIZE])
    {
    	StudentNode * root;
    	GradeNode *nextNode = proot->student.gradeList; 
    	int cmpResult;
    
    	if (proot == NULL)
    		return FALSE;
    
    	root = proot; // Used just of cosmetics
    	
    	cmpResult = CompareId(id, root->student.Id);
    
    	if (cmpResult == 0)
    	{
    		for (; nextNode->next != NULL; nextNode = nextNode->next);
    
    		nextNode->next = newGrade;
    		return TRUE;
    	}
    	
    	// Advance right of left, based on compare
    	if (cmpResult > 0)
    	{
    		return InsertGradesTreeNode(root->right, nextNode, id);
    	}		
    
    	if (cmpResult < 0)
    	{
    		return InsertGradesTreeNode(root->left, nextNode, id);
    	}
    
    	return TRUE;	
    }
    
    
    
    // ----------------------------------------------------
    //	Synopsis: Load student grade list to appropriate
    //			student node.
    //
    //	Arguments: 
    //		- grades file name
    //		- pointer for tree root
    //
    //	Return-Value: FALSE on failure. TRUE otherwise 
    // ----------------------------------------------------
    int LoadGradesFromFile(const char * filename, StudentNode * proot)
    {
    		
    	GradeNode gradeN;
    	FILE * gradesFile;
    	char id[ID_SIZE];
    	
    	// Zero gradeN in advance
    	gradeN.next = NULL;
    	gradeN.grade.grade = 0;
    	gradeN.grade.exercise = 0;
    		
    	// Open grades file
    	gradesFile = fopen(filename, "r");
    	if (gradesFile == NULL)
    	{
    		fprintf(stderr, "fopen(%s) failed\n", filename);
    		return FALSE;
    	}
    		
    	// Add each student grades in file into tree
    	while (EOF != fscanf(gradesFile, "%9s %d %d", id, gradeN.grade.grade, gradeN.grade.exercise))
    	{
    		if (!InsertGradesTreeNode(proot, &gradeN, id))
    		{
    			fprintf(stderr, "CreateGradeReport failed!\n");
    			fclose(gradesFile);
    			return FALSE;
    		}
    	}
    
    	// close and exit
    	fclose(gradesFile);
    
    	return TRUE;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're going to make a tree out of this, you have to give every student their own node (i.e. via malloc), and not keep reusing the same piece of memory over and over and over again/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-01-2009, 09:54 AM
  2. Replies: 2
    Last Post: 09-16-2009, 07:00 AM
  3. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 10:45 PM
  5. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

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