gpt4 book ai didi

c - Switch 语句 C 中的无限循环

转载 作者:行者123 更新时间:2023-11-30 15:21:32 25 4
gpt4 key购买 nike

每当选择“4”案例时,我就会陷入无限循环,它只会永远打印出该语句。每当我似乎解决这个问题时,我都无法让菜单循环,直到按下 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;
}

最佳答案

这应该可以解决问题。我将 choice 的初始值更改为 -1 (进入循环,并将 scanf 放入 main() 函数的循环中,并从 processRequest() 中删除了 case 0、5 和 6。我认为应该这样做解决你的问题

int main(){

FILE *input = fopen("scores.txt","r");
int scores[12][8]={0}, choice=-1;

if (input == NULL){
fprintf(stderr, "Can't open input file!\n");
exit(1);
}

makeArray(input,scores);
while(choice != 0){
displayMenu();
scanf("%d",&choice);
processRequest(scores,choice);
}

system("pause");
return 0;
}

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;
default:break;
}
}

希望这有帮助

关于c - Switch 语句 C 中的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29552253/

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