c - 为什么 scanf 在这里跳过输入?

标签 c while-loop scanf

#include <stdio.h>

int main()
{
    char another;
    int num;
    do
    {
        printf("enter the number");
        scanf("%d", &num);
        printf("square of%d is %d\n", num, num * num);
        printf("want to check another number y/n");
        fflush(stdin);
        scanf("%c", &another);
    } while (another == 'y');
    return 0;
}

上面的代码中,第二个scanf()没有被执行,因此控制台不接受输入。

最佳答案

根据标准,刷新stdinundefined behaviour 。请参阅Using fflush(stdin)了解更多信息。

关于c - 为什么 scanf 在这里跳过输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42910246/

相关文章:

c - DAQmxReadAnalogScalarF64 到底做什么?

c - 为什么我们在 c 中声明后使用 clrscr() 函数?

c - 使用简单函数初始化静态字符数组表示长度为 6,但它不应该是 1 吗?

php - 在 while 循环中使用 mysqli_fetch_array 时,PHP/MySQL 如何知道获取下一行?

python - 访问一个以前返回的值 - Python3.2

c - 两个整数相除结果为 0,而我应该得到 1

c - 将文件名打印到标准输出

java - 无法弄清楚当玩家得到 5 分时如何让游戏停止。

c - 使用 fscanf() 获取格式化输入

c - 在 C 中,如何读取文件并仅存储 double - 之前忽略文本?