[英]Infinite Loop in Switch Statement C
每當選擇“ 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;
}
這應該可以解決問題。 我將選擇的初始值更改為-1(進入循環,並將scanf放入main()
函數的循環中,並從processRequest()
刪除了情況processRequest()
和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;
}
}
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.