Float Point Exception trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prisms
    New Member
    • May 2010
    • 3

    Float Point Exception trouble

    For Some reason when I compile the screen says "Floating point exception". Here is my code was wondering if anyone could lend me a hand.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char file[100];
    int total = 0;
    
    struct student
    {
    	int SID;
    	int quiz[5];
    } students[40], highscore, lowscore, avg;
    
    void Textfile()
    {
    	FILE* numbers;
    	int counter = 0;
    	numbers = fopen(file, "r");
    	
    	if (numbers != NULL)
    	
    	{
    		while (!feof(numbers))
    		{
    			fscanf (numbers, "%d %d %d %d %d %d", &students[counter].SID, &students[counter].quiz[0], &students[counter].quiz[1], &students[counter]. quiz[2], &students[counter]. quiz[3], &students[counter].quiz[4]);
    			counter++;
    		}
    		
    	total = counter - 1;
    	fclose(numbers);
    	}
    	
    	else
    	{
    		printf("The could not be opened");
    	}
    }	
    		
    int dataHighScore (int quiznum)
    {
    	int highscore = -1;
    	int x = 0;
    	
    	for (x=0; x<total; x++)
    	{
    		if (x == 0)
    		{
    			highscore = students[x].quiz[quiznum];
    		}
    		
    		else
    		
    		{
    				if(highscore < students[x].quiz[quiznum])
    				highscore = students[x].quiz[quiznum];
    			}	
    		}
    		
    		return highscore;
    	}
    	
    	int dataLowScore (int quiznum)
    	{
    		int lowscore = -1;
    		int x = 0;
    		
    		for(x=0;x < total;x++)
    		{
    			if(x==0)
    			{
    				lowscore = students[x].quiz[quiznum];
    			}
    			
    			else
    			{
    				if(lowscore > students[x].quiz[quiznum])
    				lowscore = students[x].quiz[quiznum];
    			}
    		}
    		
    		return lowscore;
    	}
    	
    	int dataAvg (int quiznum)
    	{
    		int avg = 0;
    		int x = 0;
    		for (x=0;x < total; x++)
    		{
    			avg = avg + students[x].quiz[quiznum];
    		}
    		
    		avg = avg/total;
    		return avg;
    	}
    	
    	void print()
    	{
    		int x = 0;
    		printf("....................................Student Quiz Grades...................................\n");
    		printf("___________________________________________________________________________________________\n");
    		printf("Student				Quiz1			Quiz2			Quiz3			Quiz4			Quiz5\n");
    		
    		for(x=0; x<total;x++)
    		{
    			printf("%4d				%3d				%3d				%3d				%3d				%3d\n", students[x].SID, students[x].quiz[0], students[x].quiz[1], students[x].quiz[2], students[x].quiz[3], students[x].quiz[4]);
    		}
    		
    		printf("High Score			%3d				%3d				%3d				%3d				%3d\n", highscore.quiz[0], highscore.quiz[1], highscore.quiz[2], highscore.quiz[3], highscore.quiz[4]);
    		printf("Low Score			%3d				%3d				%3d				%3d				%3d\n", lowscore.quiz[0], lowscore.quiz[1], lowscore.quiz[2], lowscore.quiz[3], lowscore.quiz[4]);
    		printf("Avg Score			%3d				%3d				%3d				%3d				%3d\n", avg.quiz[0], avg.quiz[1], avg.quiz[2], avg.quiz[3], avg.quiz[4]);
    		printf("____________________________________________________________________________________________\n");
    		
    	}
    	
    	int main()
    	{
    		int x = 0;
    		highscore.SID = 9999;
    		lowscore.SID = 9998;
    		avg.SID = 9997;
    		
    		printf("Please enter the text file:\n");
    		scanf("%s", file); 
    		
    		Textfile();
    		for(x=0;x < 5; x++)
    		{
    			highscore.quiz[x] = dataHighScore(x);
    			lowscore.quiz[x] = dataLowScore(x);
    			avg.quiz[x] = dataAvg(x);
    		}
    		print();
    		return 0;
    	}
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Interestingly you appear to be doing no floating point arithmetic so when you get this sort of thing you need to check for maths errors.

    Typical errors are an accidental divide by 0 or calling a maths function with out of range values (trying to take the logarithm of a negative number for instance).

    Your code has a divide so verify that you are not dividing by 0.


    On a style point having global variables is very bad style and hinders maintenance and bug fixing. Both of your 2 global variables, file and total could easily be made local to main. Pass the variables to the functions that need them and have TextFile return a value for total. However I think this is just bad style rather than the cause of your problem.

    Comment

    Working...