我有一个问题修复这个无限循环。我已经做了一些测试,所以我很确定在读取文件时循环不是。"printReportHeading();"后面是一个for循环。我很确定这就是导致问题的原因。我该如何解决这个问题。这和二维数组的初始化有关吗?
代码: int main(void)
{
FILE* fileIn;
FILE* printFile;
float average;
char letterGrade;
int wholeArray [MAX_STUDENTS][MAX_PROFILE],
letterFreq[5];
printInstructions();
fileIn = fopen("input11.dat", "r");
if(fileIn == NULL)
{
printf("nnFILE COULD NOT BE LOCATEDnn");
}
else
{
printFile = fopen("upchurch.txt", "w");
loadData(fileIn, wholeArray);
printReportHeading();
for(int row = 0; row < MAX_STUDENTS; row++)
{
average = calcAverage(wholeArray);
letterGrade = determineLetter(average);
printLine(printFile, wholeArray, average, letterGrade);
switch(letterGrade)
{
case 'A':
letterFreq[0]++;
break;
case 'B':
letterFreq[1]++;
break;
case 'C':
letterFreq[2]++;
break;
case 'D':
letterFreq[3]++;
break;
default:
letterFreq[4]++;
break;
}
}
printHighScores(printFile, wholeArray);
printLowScores(printFile, wholeArray);
printAverageScores(printFile, wholeArray);
fprintf(printFile, "---------------------------------------------n");
printHistogram(printFile, letterFreq);
}
return;
}
/******************************************************************************
*Prints the instructions to the user*
******************************************************************************/
void printInstructions(void)
{
printf("=================================================================n");
printf("= This program takes up to 40 student's ID and five quiz grades =n");
printf("= then finds each students letter grade, average of each quiz, =n");
printf("= highest and lowest grade of each quiz, and creates a =n");
printf("= histogram of all of the students letter gradesa as a whole. =n");
printf("=================================================================nn");
return;
}
/******************************************************************************
*Gets the data from the file for the user*
******************************************************************************/
int loadData(FILE* fileIn, int wholeArray[][MAX_PROFILE])
{
for(int i = 0; i < MAX_STUDENTS; i++)
for(int j = 0; j < MAX_PROFILE; j++)
{
fscanf(fileIn, "%d", &wholeArray[i][j]);
}
return 0;
}
/******************************************************************************
*Prints the report heading*
******************************************************************************/
void printReportHeading()
{
printf("Student Quiz 1 Quiz 2 Quiz 3 Quiz 4 Quiz 5 Average Lettern");
return;
}
/******************************************************************************
*Calculates each student's average and letter grade*
******************************************************************************/
float calcAverage (int wholeArray[][MAX_PROFILE])
{
int totalGrades = 0;
for (int i = 1; i < MAX_PROFILE; i++)
{
totalGrades += *wholeArray[i];
}
return (totalGrades / 5.0f);
}
/******************************************************************************
*Finds the letter grade*
******************************************************************************/
char determineLetter(float average)
{
char letterGrade;
if (average >= 90)
letterGrade = 'A';
else if (average >= 80)
letterGrade = 'B';
else if (average >= 70)
letterGrade = 'C';
else if (average >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
/******************************************************************************
Prints a line of students information
******************************************************************************/
void printLine(FILE* printFile, int wholeArray[][MAX_PROFILE], float average, char letterGrade)
{
for(int i = 0; i <= MAX_STUDENTS; i++)
for(int j = 0; j < MAX_PROFILE; j++)
{
fprintf(printFile, "%d", wholeArray[i][j]);
if ((j = MAX_PROFILE))
{
printf("%3.2f", average);
printf("%c", letterGrade);
j = 0;
}
}
fprintf(printFile, "---------------------------------------------------n");
return;
}
/******************************************************************************
Find the high score of each quiz
******************************************************************************/
void printHighScores(FILE* printFile, int wholeArray[][MAX_PROFILE])
{
int highestTest = 0;
fprintf(printFile, "High ");
for (int i = 1; i < MAX_PROFILE; i++)
{
for(int j = 0; j < MAX_STUDENTS; j++)
{
if (highestTest < wholeArray[j][i])
{
highestTest = wholeArray[j][i];
}
}
fprintf(printFile, "%3d", highestTest);
}
return;
}
/******************************************************************************
Finds the low score of each quiz
******************************************************************************/
void printLowScores(FILE* printFile, int wholeArray[][MAX_PROFILE])
{
int lowestTest = 100;
fprintf(printFile, "Low ");
for (int i = 1; i < MAX_PROFILE; i++)
{
for(int j = 0; j < MAX_STUDENTS; j++)
{
if (lowestTest > wholeArray[j][i])
{
lowestTest = wholeArray[j][i];
}
}
fprintf(printFile, "%3d", lowestTest);
}
return;
}
/******************************************************************************
Finds the average score of each quiz
******************************************************************************/
void printAverageScores(FILE* printFile, int wholeArray[][MAX_PROFILE])
{
float averageTest = 0.0;
int i;
fprintf(printFile, "Average ");
for (i = 1; i < MAX_PROFILE; i++)
{
for(int j = 0; j < MAX_STUDENTS; j++)
{
averageTest += wholeArray[j][i];
}
averageTest = averageTest / i;
fprintf(printFile, "%5.2f", averageTest);
}
return;
}
/******************************************************************************
Prints histogram of letter frequency
******************************************************************************/
void printHistogram(FILE* printFile, int letterFreq[5])
{
for (int i = 0; i < 5; i++)
{
for (int j = 1; j <= letterFreq[i]; j++)
printf("*");
printf("n");
}
return;
}
在printLine
中,您有:
if ((j = MAX_PROFILE))
{
…
j = 0;
}
这里有两个错误。首先,j = MAX_PROFILE
是赋值,而不是比较。它将j
设置为MAX_PROFILE
,并求值为true,导致执行if
的主体。
则if
的主体将j
设置为零。这会导致内部循环无限重复。
我怀疑你这里的意图是在打印完学生资料的所有行之后再打印平均成绩和成绩。在这种情况下,只需在j
的循环之后打印它们,但仍然在i
的循环内。不需要测试,也不需要将j
设置为0
这可能不是你的bug的原因,但letterFreq
从未初始化,所以printHistogram
调用未定义的行为,并可能最终打印数十亿个"*"。解决这个问题的方法就是将数组成员初始化为0
letterFreq[5] = {0};