Switch 语句 C 中的无限循环

Infinite Loop in Switch Statement C

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;
}

这应该可以解决问题。我将选择的初始值更改为 -1(进入循环,我将 scanf 放入 main() 函数的循环中,并从 processRequest() 中删除了大小写 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;    
    }
}

希望对您有所帮助