【发布时间】:2015-04-10 02:18:07
【问题描述】:
I'm getting stuck in an infinite loop whenever the "4" case is getting selected, it just prints out the statement forever.每当我似乎得到修复时,我无法让菜单循环,直到按下 0。我觉得夹在两个不好的地方之间。
这是我的代码,谢谢!!!!:
#include <stdlib.h>
#include <stdio.h>
void makeArray(FILE *input,int scores[12][8]);
void displayMenu();
void processRequest(int scores[12][8], int choice);
int getScore(int scores[12][8],int month, int game);
int getMonthMax(int scores[12][8],int month);
float getMonthAvg(int scores[12][8],int month);
int getYearMax(int scores[12][8]);
int main(){
FILE *input = fopen("scores.txt","r");
int scores[12][8]={0}, choice=0;
if (input == NULL){
fprintf(stderr, "Can't open input file!\n");
exit(1);
}
makeArray(input,scores);
displayMenu();
scanf("%d",&choice);
while(choice != 0){
processRequest(scores,choice);
}
system("pause");
return 0;
}
void makeArray(FILE *input,int scores[12][8]){
int i,j;
for(i = 0; i < 12; i++){
for(j =0; j < 8; j++){
fscanf(input,"%d",&scores[i][j]);
}
}
fclose(input);
}
void displayMenu(){
printf("What would you like to do?\n");
printf("------------------------------------\n");
printf("Select from options 1-7 or 0 to stop\n");
printf("Select 1 to get the score for a specific game\n");
printf("Select 2 to get the max score for a specific month\n");
printf("select 3 to get the average score for a specific month\n");
printf("Select 4 to get the max score for the year\n");
printf("Select 5 to get the average score for the year\n");
printf("Select 6 to get the number of tournaments missed for the year\n");
printf("Select 7 to print all scores for the year\n");
printf("Select 0 to stop\n");
printf("------------------------------------\n");
}
void processRequest(int scores[12][8], int choice){
int month = 0, game=0, score1=0;
float score =0;
while(choice != 0){
printf("\n");
printf("\n");
printf("\n");
switch(choice){
case 1:
printf("Please enter the month and the game\n");
scanf("%d %d", &month,&game);
score1 = getScore(scores,month,game);
printf("Score for Tournament %d is %d\n",game, score1);
printf("\n");
break;
case 2:
printf("Please enter the month\n");
scanf("%d",&month);
score1 = getMonthMax(scores,month);
printf("The max score for month %d is %d\n",month,score1);
printf("\n");
break;
case 3:
printf("Please enter the month\n");
scanf("%d",&month);
score = getMonthAvg(scores,month);
printf("The average score for month %d is %.2f\n",month,score);
printf("\n");
break;
case 4:
score1 = getYearMax(scores);
printf("The maximum score for the year is %d\n",score1);
printf("\n");
return;
case 5:
case 6:
case 0:break;
default:
displayMenu();
scanf("%d",&choice);
processRequest(scores,choice);
}
}
}
int getScore(int scores[12][8],int month, int game){
int score = 0;
score = scores[month-1][game-1];
return score;
}
int getMonthMax(int scores[12][8],int month){
int score =0,j,max=scores[month-1][0];
for(j=1; j < 8;j++){
if(scores[month-1][j]>max){
max = scores[month-1][j];
}
}
return max;
}
float getMonthAvg(int scores[12][8],int month){
float avg =0;
int j,sum=0;
for(j=0;j<8;j++){
sum += scores[month-1][j];
}
avg = sum / 8.00;
return avg;
}
int getYearMax(int scores[12][8]){
int score =0,i, j,max=scores[0][0];
for(i = 0; i < 12; i++){
for(j=0; j < 8;j++){
if(scores[i][j]>max){
max = scores[i][j];
}
}
}
return max;
}
【问题讨论】:
-
while(choice != 0){ processRequest(scores,choice); }choice有什么变化?它如何达到 0 来停止你的循环? ewww - 为什么processRequest()是递归的?!?! -
我不确定...这是我最后一次尝试修复循环的剩余部分,当我自己拥有它时,我要么进入无限循环,要么没有切换到循环 - 无论哪种方式我都遇到了问题。而且我不确定递归是什么意思?! @John3136
标签: c function switch-statement infinite-loop