我写了以下代码:
#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)上显示消息&#34;未处理异常:0xC0000005:访问冲突写入位置0x7504006D。&#34;老实说,我并不确定这意味着什么。我使用CodeLite作为IDE来开发程序。任何有关我可能遇到问题的帮助或暗示都将不胜感激。谢谢!
答案 0 :(得分:0)
你的街道数组只有18个字节。根据您从add1,add2和add3,2个空格和终止0读取3次9字节的数据,您最多可以输入30个字节 假设您的数据元素适合数组的9个字节,则最后一个需要0