我在修复这个无限循环时遇到了问题。 我做了一些测试,所以我很确定循环不是在读取文件时。 就在“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("\n\nFILE COULD NOT BE LOCATED\n\n");
}
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("=================================================================\n\n");
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 Letter\n");
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;
}
答案 0 :(得分:1)
在printLine
中,您有:
if ((j = MAX_PROFILE))
{
…
j = 0;
}
这有两个问题。首先,j = MAX_PROFILE
是一项任务,而非比较。它将j
设置为MAX_PROFILE
并计算为true,从而导致执行if
的正文。
然后if
的正文将j
设置为零。这会导致内循环无限重复。
我怀疑你打算在打印学生档案的所有行后打印平均值和等级。在这种情况下,只需在j
上的循环后打印它们,仍然在i
的循环内。无需测试,也无需将j
设置为零。
答案 1 :(得分:1)
这可能不是您的错误的原因,但letterFreq
永远不会被初始化,因此printHistogram
会调用未定义的行为并最终打印出数十亿的“*”。对此的修复只是将数组成员初始化为0
letterFreq[5] = {0};