currently i am doing a program to get the input (0 to 50) from the user. If the number is out of the range then the number will not be counted. After that, i will calculate and
like this
#include
#include
#include
#include
#include
int get_int(const char *prompt){
char buff[32];
for(;;){
fputs(prompt, stdout);fflush(stdout);
if(fgets(buff, sizeof buff, stdin)){
if(strchr(buff, '\n')){
char *endp;
long n;
errno = 0;
n = strtol(buff, &endp, 10);
if(errno == 0 && *endp == '\n' && INT_MIN <= n && n <= INT_MAX)
return n;
} else {//too long input
while(getchar() != '\n');
}
} else {//input EOF
continue;//ignore EOF
}
}
}
int main(void){
int n = 0, score, min = INT_MAX, max = INT_MIN;
double total = 0, average;
printf("Enter test scores,-1 to exit\n");
while((score = get_int("* Scores[%d] (0-50) : ")) != -1){
if(score < 0 || score > 50)
continue;
total += score;
if(score > max)
max = score;
if(score < min)
min = score;
++n;
}
if(n){
average = total / n;
printf("Average marks = %.2f\n", average);
printf("MIN:%d\n", min);
printf("MAX:%d\n", max);
} else {
puts("No valid input");
}
}