Я получаю застревание в бесконечном цикле всякий раз, когда случай «4» выбирается, он просто печатает заявление навсегда. Всякий раз, когда мне кажется, что это исправлено, я не могу заставить меню зацикливаться до тех пор, пока не будет нажата 0. Я чувствую себя пойманным между двумя плохими местами.Бесконечная петля в заявлении переключателя C
Вот мой код, спасибо !!!!:
#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;
}
'в то время как (выбор! = 0) {ProcessRequest (оценки, выбор);}?' 'Как choice' когда-нибудь изменится Как он может получить 0, чтобы остановить цикл Ewww - и почему 'processRequest()' recursive?!?! – John3136
Я не уверен ... Это оставалось моей последней попыткой исправить цикл, когда у меня это было, я получал либо бесконечное петля или не получив переход к петле - в любом случае у меня была проблема м. И я не уверен, что такое рекурсивное средство ?! @ John3136 – bravesaint