Thread: program keeps crashing....

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    19

    program keeps crashing....

    This code works perfectly. but
    Code:
    //Maxwell Johnson
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>    
    //below are amount of room for the info in the structs
    #define FIRSTNAME 7
    #define MIDINITIAL 1
    #define LASTNAME 9
    #define STREET 16
    #define CITY 11
    #define STATE 2
    #define ZIP 5
    
    #define AGE 3
    #define GPA 5//includes newline space
    
    #define STUDENTNUMBER 25//avg # of students in a class
    #define MAX 100
    
    typedef struct{
    	char street[STREET + 1];//allowed to +1?
    	char city[CITY + 1];
    	char state[STATE + 1];
    	char zip[ZIP + 1];
    }Address;//first letter caps
    
    typedef struct{
    	char firstname[FIRSTNAME + 1];//+1 for /0
    	char midinitial[MIDINITIAL + 1];
    	char lastname [LASTNAME + 1];
    	Address stuaddress;
    	int age;//keep at natural datatype then in strsub read into temp string local to f() then convert using atoi/atof
    	double gpa;
    }Studentinfo;
    
    void sortIntArray(Studentinfo studentdatastructurearray[], int structindex);//probably orders GPA
    void strsub(char dataline[], char s2[], int start, int length);
    double avggpa(Studentinfo studentinfostructurearray[], int structindex);
    void printavggpa(Studentinfo studentinfostructurearray[], int structindex);
    
    
    void main(void)
    {
    	Studentinfo studentinfostructurearray[STUDENTNUMBER];
    	int structindex, subindex;
    	char dataline[100];//initialize this? it reads string from .dat
    	//see intro to files in notes
    	FILE *studentdata;//a file pointer
    	errno_t err;//something you have to do
    	
    	err = fopen_s(&studentdata, "C:\\Users\\Max\\Desktop\\c programs\\arrways\\hi\\hi\\Students.dat", "r");//r is read, open/close in diff f() other than main?
    
    	if(err != 0)
    	{
    		printf("Student.dat not found");
    		exit (1);
    	}
    
    	subindex = 0; //for the strsub f()
    	structindex = 0;
    
    	printf("Students' info of the datafile:\n");
    	while(!feof(studentdata))
    	{//use studentinfostructurearray->age if making into seperate f()??maybe
    		fgets(dataline, MAX, studentdata);//reads line in data file, stored into dataline string
    			//chop up w/strsub
    
    		strsub(dataline, studentinfostructurearray[structindex].firstname, 0, FIRSTNAME);//harcode 0 now, then define later as FIRSTNAME INDEX
    												
    		strsub(dataline, studentinfostructurearray[structindex].midinitial, 8, MIDINITIAL);
    
    		strsub(dataline, studentinfostructurearray[structindex].lastname, 10, LASTNAME);
    
    		strsub(dataline, studentinfostructurearray[structindex].stuaddress.street, 20, STREET);
    
    		strsub(dataline, studentinfostructurearray[structindex].stuaddress.city, 37, CITY);
    
    		strsub(dataline, studentinfostructurearray[structindex].stuaddress.state, 49, STATE);
    
    		strsub(dataline, studentinfostructurearray[structindex].stuaddress.zip, 52, ZIP);
    
    		studentinfostructurearray[structindex].age = atoi(&dataline[57]);
    		studentinfostructurearray[structindex].gpa = atof(&dataline[60]);
    
    
    		structindex++;
    		
    	}fclose(studentdata);
    	printavggpa(studentinfostructurearray, structindex);
    
    	sortIntArray(studentinfostructurearray, structindex);
    }
    	//final tues 5-17 2:30
    
    void sortIntArray(Studentinfo studentinfostructurearray[], int structindex) //legit parameters?:(Studentdata ->studentdatastructurearray[], int structindex)
    {  //upward doesn't need *
    	int i, j; // indexes into unsorted and sorted partitions(pieces/sections) 
        double gpaholder; // temporarily holds a part of the array(probably the whole struct)
        for (i = 1; i < structindex; ++i) { 
                gpaholder = studentinfostructurearray[i].gpa; 
                j = i - 1; 
                while (j >= 0 && gpaholder < studentinfostructurearray[j].gpa) { 
                    studentinfostructurearray[j + 1].gpa = studentinfostructurearray[j].gpa; 
                    j = j - 1; 
    			}        
                studentinfostructurearray[j + 1].gpa = gpaholder; //should .gpa be there?
    	}
    	i = 0;
    	while(i < structindex){
    		printf("........in gpa done son: %lf legit", studentinfostructurearray[i].gpa);
    		i++;
    	}
    }    
    
    void strsub(char dataline[], char s2[], int start, int length)
    {
    	int index = 0;
    	while(index != length)//start != (length))
    	{
    		s2[index] = dataline[start];
    		start++;
    		index++;//start++ as long as <= length
    	}
    	s2[index] = '\0';
    }
    
    void printavggpa(Studentinfo studentinfostructurearray[], int structindex)
    {
    	int index, minage, minageindex = 0, bestgpaindex;
    	double gpaavg = avggpa(studentinfostructurearray, structindex);
    	double bestgpa;
    
    	//bestgpa()
    	bestgpa = 0.0;//lowest gpa possible
    	for(index = 0; index < structindex; index++){
    		if(studentinfostructurearray[index].gpa > bestgpa){
    			bestgpa = studentinfostructurearray[index].gpa;
    			bestgpaindex = index;
    		}
    	}
    	printf("Best gpa:%s %s %s\n", studentinfostructurearray[bestgpaindex].firstname, studentinfostructurearray[bestgpaindex].midinitial, studentinfostructurearray[bestgpaindex].lastname);
    
    	//higheravggpa()
    	printf("Students with higher than average gpa\n");
    	for(index = 0; index < structindex; index++){
    		if(studentinfostructurearray[index].gpa > gpaavg)
    			printf("%s %s %s \n", studentinfostructurearray[index].firstname, studentinfostructurearray[index].midinitial, studentinfostructurearray[index].lastname);
    	}
    
    	//youngestlowgpa
    	minage = studentinfostructurearray[index].age;
    	for(index = 0; index < structindex; index++){
    		if(studentinfostructurearray[index].gpa < gpaavg)
    		{
    			if(studentinfostructurearray[index].age < minage){
    				minage = studentinfostructurearray[index].age;
    				minageindex = index;
    			}
    		}
    	}
    	
    	printf("Youngest person with a gpa below avg:%s %s %s", studentinfostructurearray[minageindex].firstname, studentinfostructurearray[minageindex].midinitial, studentinfostructurearray[minageindex].lastname);
    }
    
    double avggpa(Studentinfo studentinfostructurearray[], int structindex)
    {
    	double avggpa, totalstudents = 0.0, total = 0.0;
    	int index;
    	for(index = 0; index < structindex; index++){
    		total+=studentinfostructurearray[index].gpa;
    		totalstudents+=1.0;
    	}
    	avggpa = total / totalstudents;
    
    	return avggpa;
    }
    this one prints out ANNA 0. then crashes. i'm going insane trying to figure out why.
    Code:
    //Maxwell Johnson
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>    
    //below are amount of room for the info in the structs
    #define FIRSTNAME 7
    #define MIDINITIAL 1
    #define LASTNAME 9
    #define STREET 16
    #define CITY 11
    #define STATE 2
    #define ZIP 5
    
    #define AGE 3
    #define GPA 5//includes newline space
    
    #define STUDENTNUMBER 25//avg # of students in a class
    #define MAX 100
    
    typedef struct{
    	char street[STREET + 1];//allowed to +1?
    	char city[CITY + 1];
    	char state[STATE + 1];
    	char zip[ZIP + 1];
    }Address;//first letter caps
    
    typedef struct{
    	char firstname[FIRSTNAME + 1];//+1 for /0
    	char midinitial[MIDINITIAL + 1];
    	char lastname [LASTNAME + 1];
    	Address stuaddress;
    	int age;//keep at natural datatype then in strsub read into temp string local to f() then convert using atoi/atof
    	double gpa;
    }Studentinfo;
    
    void readfile(int *structindex, Studentinfo studentinfostructurearray[]);
    //void sortIntArray(Studentinfo studentdatastructurearray[], int structindex);//probably orders GPA
    void strsub(char dataline[], char s2[], int start, int length);
    //double avggpa(Studentinfo studentinfostructurearray[], int structindex);
    //void printavggpa(Studentinfo studentinfostructurearray[], int structindex);
    
    
    void main(void)
    {
    	Studentinfo studentinfostructurearray[STUDENTNUMBER];
    	int structindex = 0;
    	//char dataline[100];//initialize this? it reads string from .dat
    	readfile(&structindex, studentinfostructurearray);
    	
    	//printavggpa(studentinfostructurearray, *structindex);
    
    	//sortIntArray(studentinfostructurearray, *structindex);
    	
    
    }
    	//final tues 5-17 2:30
    
    void readfile(int *structindex, Studentinfo studentinfostructurearray[])
    {
    	char dataline[100];//initialize this? it reads string from .dat
    	//new f(1)
    	FILE *studentdata;//a file pointer
    	errno_t err;//something you have to do
    	
    	err = fopen_s(&studentdata, "C:\\Users\\Max\\Desktop\\c programs\\arrways\\hi\\hi\\Students.dat", "r");//r is read, open/close in diff f() other than main?
    
    	if(err != 0)
    	{
    		printf("Student.dat not found");
    		exit (1);
    	}
    
    	printf("Students' info of the datafile:\n");
    
    	while(!feof(studentdata))
    	{//use studentinfostructurearray->age if making into seperate f()??maybe
    		
    		
    		fgets(dataline, MAX, studentdata);//reads line in data file, stored into dataline string
    			//chop up w/strsub
    		
    		strsub(dataline, studentinfostructurearray[*structindex].firstname, 0, FIRSTNAME);//harcode 0 now, then define later as FIRSTNAME INDEX
    		printf("%s %d ", studentinfostructurearray[*structindex].firstname, *structindex);
    												
    		strsub(dataline, studentinfostructurearray[*structindex].midinitial, 8, MIDINITIAL);
    
    		strsub(dataline, studentinfostructurearray[*structindex].lastname, 10, LASTNAME);
    
    		strsub(dataline, studentinfostructurearray[*structindex].stuaddress.street, 20, STREET);
    
    		strsub(dataline, studentinfostructurearray[*structindex].stuaddress.city, 37, CITY);
    
    		strsub(dataline, studentinfostructurearray[*structindex].stuaddress.state, 49, STATE);
    
    		strsub(dataline, studentinfostructurearray[*structindex].stuaddress.zip, 52, ZIP);
    		
    		studentinfostructurearray[*structindex].age = atoi(&dataline[57]);
    		studentinfostructurearray[*structindex].gpa = atof(&dataline[60]);//review pointers/pbr
    
    
    		*structindex++;
    		
    	}fclose(studentdata);
    
    }
    
    void strsub(char dataline[], char s2[], int start, int length)
    {
    	int index = 0;
    	while(index != length)//start != (length))
    	{
    		s2[index] = dataline[start];
    		start++;
    		index++;//start++ as long as <= length
    	}
    	s2[index] = '\0';
    }
    thanks,
    Max

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Don't use feof() to control the loop. Question 12.2
    Where is the line that's crashing? Did you try to use a debugger?
    Don't just throw the code and say it's crashing!


    Code:
    		*structindex++;
    Look up C operator precedence.
    It'd be better to use temporary counter and assign to structindex when your function has finished.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    19
    it was precedence issues, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program keeps crashing
    By Charak in forum C Programming
    Replies: 5
    Last Post: 03-23-2011, 07:07 AM
  2. crashing program, help
    By xniinja in forum Windows Programming
    Replies: 1
    Last Post: 07-07-2010, 03:57 PM
  3. crashing program, help
    By xniinja in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 11:42 AM
  4. Program crashing, Need help!
    By Obsidian_179 in forum C Programming
    Replies: 3
    Last Post: 06-19-2008, 05:26 PM
  5. help plz crashing program
    By mill1000 in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 09:15 AM