Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 崩溃问题,可能是由于for循环_C - Fatal编程技术网

C 崩溃问题,可能是由于for循环

C 崩溃问题,可能是由于for循环,c,C,我的程序在运行时不断崩溃。我已经隔离了它的一部分(使用/**/)来尝试找出问题所在,我认为这与排序函数中的第二个for循环有关,因为隔离可以防止崩溃。然而,我尝试了几种不同的方法(使用while/do循环等)来修复它,但它还是成功地持续崩溃。我还查看了参数以及我在main中是如何声明的,但我看不出有什么问题。了解我,这可能是我几个小时来一直在努力修复的一件非常愚蠢的事情。任何帮助都将不胜感激 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h

我的程序在运行时不断崩溃。我已经隔离了它的一部分(使用/**/)来尝试找出问题所在,我认为这与排序函数中的第二个for循环有关,因为隔离可以防止崩溃。然而,我尝试了几种不同的方法(使用while/do循环等)来修复它,但它还是成功地持续崩溃。我还查看了参数以及我在main中是如何声明的,但我看不出有什么问题。了解我,这可能是我几个小时来一直在努力修复的一件非常愚蠢的事情。任何帮助都将不胜感激

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 5

struct student
{
    char name[20];
    int hw1, hw2, hw3, ex1, ex2, totalhw, totalex;
    float classperc;
    char grade;
};


void student_info(struct student s[], int n, int *classex1, int *classex2, int *a, int *b, int *c, int *d, int *f)
{
    for (int i = 0; i < n; i++)
    {
        printf("\n\nPlease enter the student's name:\n");
        gets_s(s[i].name, 20);

        printf("\nPlease enter the student's homework grades:\n");
        scanf("%d %d %d", &(s[i].hw1), &(s[i].hw2), &(s[i].hw3));

        printf("\nPlease enter the student's exam scores:\n");
        scanf("%d %d", &(s[i].ex1), &(s[i].ex2));
        getchar();

        s[i].totalhw = s[i].hw1 + s[i].hw2 + s[i].hw3;  
        s[i].totalex = s[i].ex1 + s[i].ex2;

        *classex1 += s[i].ex1;
        *classex2 += s[i].ex2;

        s[i].classperc = ((float)s[i].totalhw / 1.875) + ((float)s[i].totalex / 3.333);

        if (s[i].classperc >= 90)
        {
            *a = *a + 1;
            s[i].grade = 'A';
        }

        else if (s[i].classperc >= 80)
        {
            *b = *b + 1;
            s[i].grade = 'B';
        }

        else if (s[i].classperc >= 70)
        {
            *c = *c + 1;
            s[i].grade = 'C';
        }

        else if (s[i].classperc >= 60)
        {
            *d = *d + 1;
            s[i].grade = 'D';
        }

        else
        {
            *f = *f + 1;
            s[i].grade = 'F';
        }
    }
}


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

    for (int i = 0; i < n; i++)
    {
        printf("\nStudent: %s\nThe Three Homework Scores: %d %d %d\nThe Two Exam Scores: %d %d\n", s[i].name, s[i].hw1, s[i].hw2, s[i].hw3, s[i].ex1, s[i].ex2);
        printf("Total Homework Score: %d\nTotal Exam Score: %d\nClass Percentage: %f  Grade: %s", s[i].totalhw, s[i].totalex, s[i].classperc, s[i].grade); 
// It crashes right before executing this second printf statement (I have no idea why :[)
    }
}


void avg_exams(int classex1, int classex2, float *avgex1, float *avgex2)
{
    *avgex1 = classex1 / (float)5;
    *avgex2 = classex2 / (float)5;
}

void print_classinfo(float avgex1, float avgex2, int a, int b, int c, int d, int f)
{
    printf("\n\nThe Average Exam Score for Exam 1 is: %0.2f\nThe Average Exam Score for Exam 2 is: %0.2f\n", avgex1, avgex2);
    printf("There were %d A's, %d B's, %d C's, %d D's, %d F's in the class overall\n\n", a, b, c, d, f);
}

void main()
{
    struct student s[SIZE];
    int a, b, c, d, f , classex1, classex2;
    a = b = c = d = f = 0;
    classex1 = classex2 = 0;
    float classperc, avgex1, avgex2;

    student_info( s, SIZE, &classex1, &classex2, &a, &b, &c, &d, &f);
    sort(s, SIZE);

    avg_exams(classex1, classex2, &avgex1, &avgex2);
    print_classinfo(avgex1, avgex2, a, b, c, d, f);

    system("PAUSE");
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
#定义尺寸5
结构学生
{
字符名[20];
int hw1、hw2、hw3、ex1、ex2、totalhw、totalex;
浮动类PERC;
煤焦品位;
};
无效学生信息(结构学生s[],int n,int*classex1,int*classex2,int*a,int*b,int*c,int*d,int*f)
{
对于(int i=0;i=90)
{
*a=*a+1;
s[i]。等级='A';
}
如果(s[i].classperc>=80),则为else
{
*b=*b+1;
s[i]。等级='B';
}
如果(s[i].classperc>=70),则为else
{
*c=*c+1;
s[i]。等级='C';
}
如果(s[i].classperc>=60),则为else
{
*d=*d+1;
s[i]。等级='D';
}
其他的
{
*f=*f+1;
s[i]。等级='F';
}
}
}
无效排序(结构学生s[],整数n)
{
结构学生临时工;
对于(int i=0;i0)
{
温度=s[i];
s[i]=s[j];
s[j]=温度;
}
}
}
对于(int i=0;i
检查您的
printf()
格式代码。您可以将其分成更小的块进行调试,以查看
printf()
的哪一部分工作异常


我不认为崩溃是由于
for
循环造成的。

逐个读取编译器警告并开始解决。
grade
被声明为
char
类型,
s[I]的格式说明符应该是什么.grade
%s
-->
%c
。grade是一个字符,但是你用格式代码
%s
而不是
%c
打印它。它与你的
for
循环无关。如果你在编译时有警告,你的编译器会警告你这个错误。我想我是个白痴,谢谢朋友们!