【发布时间】:2012-10-11 09:20:22
【问题描述】:
我不明白这个错误告诉我什么。当我运行程序时,它应该不断循环,直到用户输入 N(不再/退出)。我不明白发生了什么事。在第一组输入之后,在提示用户“你想处理一个新学生”之前,程序会抛出该异常。
我使用 Geany(它没有给我任何错误定义,它只是崩溃)和 Visual Studio 2010 运行它。
有人可以帮忙吗?
void draw_bar_chart(int student_id[], int percentage[], int size)
{
FILE *report;
int i, j=0, min = 1000000, max = 0, min_pos, max_pos, l;
char s[20];
report = fopen("report2.txt", "a");
fprintf(report, "\n******* BAR CHART (YEARLY TOTAL EXPENSES: YEARLY TOTAL INCOME) *******\n\n");
for (i = 0; i < size; i++)
{
fprintf(report, "%d%c", student_id[i], ' ');
if (percentage[i] > 0){
l = percentage[i]/10; //the lenght of the bar
if ((percentage[i]%10) > 0)
l++;
for (j = 0; j < l; j++)
s[j] = 'x';
s[l+1] = '\0';
} else {
s[0] = '!';
s[1] = '\0';
}
fprintf(report, "%-20s%6c%d%c\n", s, ' ', percentage[j], '%');
if (percentage[j] >= 0)
{
if (percentage[j] < min)
{
min = percentage[j];
min_pos = j;
}
if (percentage[j] > max)
{
max = percentage[j];
max_pos = j;
}
}
}
fprintf(report, "***lowest percentage: %d%c (student ID: %d)\n", min, '%', student_id[min_pos]);
fprintf(report, "***highest percentage: %d%c (student ID: %d)\n", max, '%', student_id[max_pos]);
fclose(report);
}
【问题讨论】:
-
用调试器单步调试你的代码找出问题所在,然后把相关代码贴在这里。
-
如果您确定代码在循环中崩溃,您可以发布您的循环代码
-
已添加循环,但我不确定这是否是问题所在。
-
您正在使用许多不同的索引变量对数组元素进行大量访问。您是否使用调试器逐步完成此操作,以确保您没有访问超出数组范围的任何元素??
-
这是您遇到代码问题时要做的第一件事。您正在使用您所说的 Visual Studio - 查看任何帮助文档,以告诉您如何设置断点以及如何逐行执行代码。这使您可以一次执行一行代码,这使您可以查看变量的值并帮助识别错误。它还将允许您找到发生访问冲突的位置。使用调试器对于编程至关重要 - 值得花时间学习如何去做。
标签: c visual-studio-2010 geany