【问题标题】:Computing Minimum, Maximum, and Average Test Score Input Validation计算最小、最大和平均测试分数输入验证
【发布时间】:2015-05-25 15:06:40
【问题描述】:

大家好,我有一个 c 编程作业,上面写着:

" 编写一个输入整数测试分数列表的 C 程序 介于 0 和 100 之间,并计算最高分、最低分 分,以及平均分。使用 -1 作为哨兵,标记结束 输入。拒绝任何超出 0 到 100 范围的测试分数。你的程序 还应该检测并拒绝错误的输入,例如字母和字符。 您应该有一个读取和验证输入测试分数的函数 并拒绝无效和错误的输入。这是一个示例运行“

我写了一个代码,但我的问题是如何拒绝像字符这样的输入..:

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

希望你能帮到我

【问题讨论】:

  • 请用正确的缩进格式化代码。您没有检查每个值
  • scanf%d 如果输入了无效的输入(如字符),则返回 0。因此,如果 status 为 0,则 scanf 失败。因此,您必须清除stdin并再次提示用户,直到输入有效的输入。

标签: c max min


【解决方案1】:

scanf 用于多个输入时,您必须注意几个条件。这些是matching 失败和read 失败。检查scanf 返回可以识别两者。在while循环中接受重复整数输入的情况下,如果出现匹配失败,还必须确保清空stdin以防止死循环。有几种方法,但最直接的整数输入方法是在再次使用 scanf 之前简单地使用 getcharstdin 中读取所有 chars

以下是实现此方法的代码的快速示例:

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

输出

$ ./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

【讨论】:

    【解决方案2】:

    也许您可以先将输入作为字符串,然后使用 strtol 检查无效字符?

    Input validation of an Integer using atoi()中的答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      相关资源
      最近更新 更多