gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:31:54 24 4
gpt4 key购买 nike

我的程序在运行时不断崩溃。我隔离了它的一部分(使用/**/)来尝试找出问题所在,我认为这与我的排序函数中的第二个 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");
}

最佳答案

检查您的 printf() 格式代码。您可以将其分成更小的 block 进行调试,以查看 printf() 的哪一部分工作异常。

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

关于崩溃问题,可能是由于 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50377667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com