【问题标题】:C programming - Unable to read user input immediatelyC 编程 - 无法立即读取用户输入
【发布时间】:2022-01-11 07:32:22
【问题描述】:

我正在编写一个程序,要求用户输入两个数字之间的数字。一个最小值和一个最大值。用户必须在这些参数中输入数字,程序才能成功运行。

这是调用 getIntFromRange 的函数。

void test04_getIntFromRange(void)
{
    int intValue;

    printf("TEST #4 - Instructions:\n"
        "1) Enter the number '14' [ENTER]\n"
        ":>");

    // You may want to comment the next line if you have not yet created the getInteger function:
    intValue = getIntFromRange(-40, 14);

    printf("////////////////////////////////////////\n");
    printf("TEST #4 RESULT: ");
    if (intValue == 14)
    {
        printf("*** PASS *** \n");
    }
    else
    {
        printf("### FAILED ###\n");
    }
    printf("////////////////////////////////////////\n\n");
}

这是我为getIntFromRange写的代码

int getIntFromRange(int lower_bound, int upper_bound)
{
    int value = 0;
    scanf("%d", &value);

    while (scanf("%d", &value) != 1) {
        printf("Error: Value must be an integer: ");
        scanf("%*s");
        clearStandardInputBuffer();
    }

    while (value > upper_bound || value < lower_bound) {
        printf("ERROR: Value must be between %d and %d inclusive: ", lower_bound, upper_bound);
        scanf("%d", &value);
    }
}

问题是当用户输入14时它没有立即注册。在注册代码之前我必须多次按Enter。这是程序通过前的输出。

TEST #4 - Instructions:
1) Enter the number '14' [ENTER]
:>14

14
////////////////////////////////////////
TEST #4 RESULT: *** PASS ***
////////////////////////////////////////

Assignment #1 Milestone #1 completed!

【问题讨论】:

  • getIntFromRange 读取一个数字两次,一次是在没有错误检查的开头,另一次是在while 循环条件中。第一个被丢弃。
  • ChecksOverStripes,为什么scanf("%*s");clearStandardInputBuffer(); 之前?

标签: c input range


【解决方案1】:

好的,首先。您正在读取输入两次。一次是在函数的开头,第二次是在这段代码中阅读它(在 while 循环条件下):

while (scanf("%d", &value) != 1) {
    printf("Error: Value must be an integer: ");
    scanf("%*s");
    clearStandardInputBuffer();
}

你可以删除这行代码:

scanf("%d", &amp;value);

就在函数的开头,应该可以正常工作。此外,您可能希望在上述代码块中重复使用 scanf() 函数后立即使用该 clearStandardInputBuffer() 函数,因此这些代码行的正确顺序是:

while (scanf("%d", &value) != 1) {
    clearStandardInputBuffer();
    printf("Error: Value must be an integer: ");
    scanf("%*s");
}

所以你的getIntFromRange 函数看起来像这样:

int getIntFromRange(int lower_bound, int upper_bound)
{
    int value = 0;

    
    while (scanf("%d", &value) != 1) {
        clearStandardInputBuffer();
        printf("Error: Value must be an integer: ");
        scanf("%*s");
    }
    while (value > upper_bound || value < lower_bound) {
        printf("ERROR: Value must be between %d and %d inclusive: ", lower_bound, upper_bound);
        scanf("%d", &value);
        
    }
}

即使您的scanf() 函数在while 循环中,它仍然会要求您输入。因此,您的函数将在变量 value 初始化之后请求输入,然后在 while 循环中第二次。

您可能还应该考虑在第二个 while 循环中添加另一个错误的输入处理,例如清除缓冲区,因为您的用户也可以在第二个 while 循环中输入错误的输入,例如输入 w 字符。如果你的用户会这样做,你可以有一个无限循环。

另外 - 如果你的 clearStandardInputBuffer() 函数正确完成,你不需要在第一个 while 循环中使用 scanf("%*s")

【讨论】:

  • 感谢您的帮助!代码现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多