2

while ループを使用して scanf のプログラムのエラー チェックを実行しようとしていますが、これを行う方法がわかりません。

ユーザーが審査員の数を 4 から 8 の間で入力し、スコアが 0 未満または 10 を超えないようにしたいと考えています。

誰かが私を助けてくれませんか?

ありがとう!

#include <stdio.h>

int i,j,k, judges;
float max, min,total;

int main(){
   max = 0.0;
   min = 10.0;
   judges = 0;
   total = 0;

printf("Enter number of judges between 4-8: ");
scanf("%d", &judges);

    float scores[judges];

for(i = 0; i < judges; i++){
    printf("Enter a score for judge %d : ", i + 1);
    scanf("%5f", &scores[i]);
}
for(j = 0; j < judges; j++){
    if(min > scores[j]){
        min = scores[j];
    }
    if (max < scores[j]){
        max = scores[j];
    }
}
for(k = 0; k < judges; k++){
    total = total + scores[k];
}
total = total-max-min;
printf("max = %4.1f    min = %4.1f    total = total = %4.1f", min,   max,total);
}
4

2 に答える 2

0

次のことを試してください。

// for judge
while (1) {
    printf("Enter number of judges between 4-8: ");
    scanf("%d", &judges);
    if (judges >= 4 && judges <= 8)
        break;
    else
        puts("Judge out of range of 4 and 8.");
}

// for socres
for(i = 0; i < judges; i++){
    while (1) {
        printf("Enter a score for judge %d : ", i + 1);
        scanf("%5f", &scores[i]);
        if (scores[i] >= 0.0 && scores[I] <= 10.0)
            break;
        else
            puts("score out of range of 0.0 and 10.0");
}
于 2016-04-17T08:45:42.153 に答える