gpt4 book ai didi

代码不要求输入字符

转载 作者:行者123 更新时间:2023-11-30 14:55:48 25 4
gpt4 key购买 nike

#include "stdio.h"
#include "stdlib.h"

int main() {

int a = 10;
float f;
double d;
char k = 'X';

scanf("%d", &a);
printf("%d\n", a);


scanf("%f", &f);
printf("%f\n", f);

scanf("%lf" , &d);
printf("%lf\n", d);

fflush(stdin);

scanf("%c",&k);
printf("%c\n",k);



return 0;
}

当我执行代码时,我只能输入数据,直到输入 double 值,然后程序退出而不要求另一个输入。有人可以解释一下这是怎么回事吗?我一直在查看在线教程,并且相同的代码适用于它们!

这是我执行程序时得到的结果:

   21
21
333.264765
333.264771
2317.23
2317.230000


Program ended with exit code: 0

最佳答案

fflush通常用于刷新输出缓冲区(输出流)。fflush(stdin) 可能会导致未定义的行为。并非所有编译器都支持此操作。 This link may help

关于代码不要求输入字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45476480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com