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

时间:2018-05-16 18:21:26

标签: c

我的程序在运行时不断崩溃。我已经隔离了它的部分(使用/ ** /)来试图弄清楚问题是什么,我认为它与我的sort函数中的第二个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");
}

1 个答案:

答案 0 :(得分:0)

查看您的printf()格式代码。您可以将其拆分为较小的块以进行调试,以查看printf()的哪个部分出乎意料地工作。

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