0

hey guys I have an assignment in programming in c which says :

" Write a C program that inputs a list of integer test scores between 0 and 100, and computes the maximum score, the minimum score, and the average score. Use -1, as a sentinel, to mark the end of the input. Reject any test score outside the range 0 to 100. Your program should also detect and reject faulty input, such as letters and characters. You should have a function that reads and validates an input test score and rejects invalid and faulty input. Here is a sample run "

I wrote a code but my problem is how to reject input like characters ..:

#include <stdio.h>
#define sentinel -1     // to end the program

int main ()
{
    int status, score, sum, i, max, min;
    double avrg;
    i = 0;          /* the counter */
    sum = 0;            // the total scores
    printf (" Enter the score , %d to termenate > ", sentinel);
    status = scanf ("%d", &score);  // checking the validity of the input

    max = score;        // the initial maximum
    min = score;        // the initial minimum

    while (score != sentinel) {

        while (score < 0 || score > 100)    // no negative or more than 100 score

        {
            printf ("The number is out of the range ; try again> ");
            scanf ("%d", &score);
        }

        sum = sum + score;
        i = i + 1;
        printf (" Enter the score , %d to termenate > ", sentinel);
        status = scanf ("%d", &score);
        if (score > max && score < 100)
            max = score;
        if (score < min && score > 0)
            min = score;
    }

    if (i != 0) {
        avrg = (double) sum / i;    // the sum of scores over the number of scores
        printf (" The avarage is : \n %.2f ", avrg);
        printf ("\n");
        printf (" Maximum score is : \n %d", max);
        printf ("\n");
        printf ("Minmum score is : \n %d", min);
    } else          // when the user ends the program without entering any value
    {
        printf ("You didn't enter any score");
    }

    return 0;
}

I hope you can help me

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Please format the code with the right indentation. You are not checking every value – Ed Heal Mar 22 '15 at 15:57
  • `scanf` with `%d` returns 0 if an invalid input(like characters) was entered. So, if `status` is 0, `scanf` failed. Therefore, you must Clear the `stdin` and prompt the user again until a valid input is entered. – Spikatrix Mar 22 '15 at 16:01

2 Answers2

1

When using scanf for multiple inputs, there are several conditions you have to watch out for. Those are matching failures and read failures. Checking the scanf return allows you to identify both. In the case of accepting repeated integer input in a while loop, if matching failure occurs, you must also insure that you empty stdin to prevent an endless loop. There are several approaches, but the most straight forward with integer input is to simply use getchar to read all chars from stdin before using scanf again.

Below is a quick example of your code implementing this approach:

#include <stdio.h>
#include <limits.h>     /* for INT_MAX */

#define SENTINEL -1     /* value to end the program         */
#define MAXSCORE 100    /* max input for score allowed      */

int main ()
{
    int status = 0;     /* always initialize you variables  */
    int score = 0;
    size_t sum = 0;     /* size_t for non-negative numbers  */
    size_t idx = 1;
    size_t max = 0;
    size_t min = INT_MAX;
    double avrg = 0.0;
    int c = 0;

    printf ("\n Enter scores [ '%d' to end entry ]\n", SENTINEL);

    while (printf ("\n  score: ") && (status = scanf ("%d", &score)) != -1)
    {
        /* check for matching failure, empty input buffer */
        if (status == 0) do { c = getchar(); } while ( c != '\n' && c != EOF);

        if (score == SENTINEL) break;
        if (score > MAXSCORE || score < 0) continue;

        if (score > max) max = score;
        if (score < min) min = score;

        sum += score;
        avrg = (double)sum/idx++;

        printf ("\n    minimum : %3zu\n    maximum : %3zu\n    average : %.2f\n", min, max, avrg);
    }

    printf ("\n Input complete\n\n");

    if (idx > 1)
        printf ("  number of scores : %3zu    Max score : %3zu    Min score : %3zu    Avg : %.2f\n\n",
                idx - 1, max, min, avrg);
    return 0;
}

Output

$ ./bin/grades

 Enter scores [ '-1' to end entry ]

  score: 77

    minimum :  77
    maximum :  77
    average : 77.00

  score: 88

    minimum :  77
    maximum :  88
    average : 82.50

  score: 83

    minimum :  77
    maximum :  88
    average : 82.67

  score: -88

  score: 108

  score: G

  score: -1

 Input complete

  number of scores :   3    Max score :  88    Min score :  77    Avg : 82.67
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

Maybe you can get input as a string first, and then check for invalid characters using strtol?

Such as the answer in Input validation of an Integer using atoi()

daxter1992
  • 468
  • 3
  • 11