2014-12-07 71 views
-3

我寫了下面的代碼將停止:程序編譯,但是當我嘗試運行

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#include<math.h> 

struct address 
{ 
char street[18]; 
char city[13]; 
char state[7]; 
char zip[5]; 
}; 
struct student 
{ 
char first_name[7]; 
char initial; 
char last_name[9]; 
struct address add; 
int age; 
double gpa; 
}; 

void readData(struct student students[], int *n); 
void print(struct student students[], int n); 
void bestGPA(struct student students[], int n); 
double averageGPA(struct student students[], int n); 
void printAboveGPA(struct student students[], int n, double avg); 
void youngestStudent(struct student students[], int n, double avg); 
void sort(struct student students[], int n); 

int main() 
{ 
struct student students[20]; 
int n=0; 

readData(students, &n); 

print(students, n); 

bestGPA(students, n); 

double avg = averageGPA(students, n); 
printf("Average GPA is: %.2lf\n", avg); 

printAboveGPA(students, n, avg); 

youngestStudent(students, n, avg); 

sort(students, n); 

printf("\nNow sorted based on GPA: \n"); 
print(students, n); 

return 0; 

} 


void readData(struct student students[], int *d) 
{ 
int n = 0; 
FILE *fp; 
fp = fopen("Students.dat", "r"); 

if(fp == NULL) { 
printf("Students.dat file not found.\n"); 
exit(1); 
} 

else 
{ 
while(fscanf(fp, "%s %c %s", 
students[n].first_name, 
&students[n].initial, 
students[n].last_name) == 3) 
{ 

char add1[10], add2[10], add3[10]; 
fscanf(fp, "%s %s %s", add1, add2, add3); 

strcpy(students[n].add.street, add1); 
strcat(students[n].add.street, " "); 
strcat(students[n].add.street, add2); 
strcat(students[n].add.street, " "); 
strcat(students[n].add.street, add3); 

fscanf(fp, "%s %s %s %d %lf", students[n].add.city, students[n].add.state, students[n].add.zip,&students[n].age, &students[n].gpa); 

n++; 
} 
} 

*d = n; 
} 

void print(struct student students[], int n) 
{ 
int i; 
for(i=0; i<n; i++) 
{ 
printf("%s %c %s %s %s %s %s %d %.2lf\n" 
, students[i].first_name 
, students[i].initial 
, students[i].last_name 
,students[i].add.street 
,students[i].add.city 
,students[i].add.state 
,students[i].add.zip 
,students[i].age 
,students[i].gpa); 
} 
} 

void bestGPA(struct student students[], int n) 
{ 
struct student s = students[0]; 

int bestGPAIndex = 0; 
int i; 
for(i=1; i<n; i++) 
{ 
if(students[bestGPAIndex].gpa < students[i].gpa) 
{ 
bestGPAIndex = i; 
} 
} 

printf("\n\nThe student with best GPA is: %c %s %s\n\n", students[bestGPAIndex].initial, students[bestGPAIndex].first_name,students[bestGPAIndex].last_name); 
} 

double averageGPA(struct student students[], int n) 
{ 
int i; 

double total; 
double avg; 

for(i=0; i<n; i++) 
{ 
total += students[i].gpa; 
} 

avg = total/n; 

return avg; 
} 

void printAboveGPA(struct student students[], int n, double avg) 
{ 
int i; 
printf("\nStudents with GPA more than average: \n"); 

for(i=0; i<n; i++) 
{ 
if(students[i].gpa > avg) 
{ 
printf("%c %s %s\n", students[i].initial, students[i].first_name, students[i].last_name); 
} 
} 
} 

void youngestStudent(struct student students[], int n, double avg) 
{ 
int i; 
printf("\nYoungest student with GPA less than average: "); 

int youngestStudentIndex=students[0].age; 
int isFound = 0; 



for(i=0; i<n; i++) 
{ 
if(students[i].gpa < avg) 
{ 
if(!isFound) 
{ 
isFound = 1; 
youngestStudentIndex = i; 
} 

if(students[youngestStudentIndex].age >= students[i].age) 
{ 
youngestStudentIndex = i; 
} 

} 
} 

if(isFound) 
printf("%c %s %s\n\n", students[youngestStudentIndex].initial, students[youngestStudentIndex].first_name, students[youngestStudentIndex].last_name); 
else 
printf("Not found !!\n\n"); 
} 

void sort(struct student students[], int n) 
{ 
int i,j; 
for (i = 0 ; i < (n - 1); i++) 
{ 
for (j = 0 ; j < n - i - 1; j++) 
{ 
if (students[j].gpa > students[j+1].gpa) 
{ 
struct student s = students[j]; 
students[j] = students[j+1]; 
students[j+1] = s; 
} 
} 
} 
} 

我得到的是students.exe已經停止,當我去運行工作。嘗試在Visual Studio中進行調試時,在test.exe中出現「0x76F3D062(ntdll.dll)未處理異常:0xC0000005:訪問衝突寫入位置0x7504006D」。老實說,我不確定這意味着什麼。我使用CodeLite作爲IDE來開發程序。任何幫助或暗示我可能有問題的地方將不勝感激。謝謝!

+2

http://stackoverflow.com/help/mcve – 2014-12-07 14:36:12

+0

但會導致程序停止工作?另外,我不能讓街頭陣列變大嗎?這樣做不能解決問題。 – user3727648 2014-12-07 15:06:40

+0

你的輸入文件(Students.dat)是什麼樣的......也許是它的一個片段。在閱讀該文件中的特定行時,你的程序是否總是死掉? – TonyB 2014-12-08 03:36:37

回答

0

你的街道數組只有18個字節。你根據你讀取的數據放置30個字節3次9個字節add1,add2和add3,2個空格和終止0 - 假設你的數據元素符合數組的9個字節最後一個是需要的0

相關問題