problems with scanf(quot;%dquot;,amp;i)(scanf(%d,amp;i) 的问题)
问题描述
对于此代码:
int i;
scanf("%d
",&i);
在我输入两个数字之前,我无法停止我的程序.
I am not able to stop my program until I input two numbers.
我觉得很奇怪,我知道什么时候输入合适,scanf会返回1.当我输入12a 'Enter'"、12 'Enter'2"等时,没关系,i = 12,似乎当我输入不同的int或输入'Enter'和另一个时,scanf 返回 1.
I think it is very strange ,I know when the input is suitable,the scanf will return 1. When I input "12a 'Enter'","12 'Enter'2" and so on ,it is ok,the i=12,it seems that when I input something is different int or input a 'Enter' and something another,the scanf returns 1.
我错过了什么?
推荐答案
"当我使用 scanf("%d
",& 时,我必须输入两个数字才能停止我的程序;i);
"
虽然这种格式使 scanf
读取数字并将其存储到 i
中,但这种读取"会继续并持续到非空白字符后跟
找到代码>.这就是输入
1 2
使 scanf
停止的原因.
"I am not able to stop my program until I input two numbers when I use scanf("%d
",&i);
"
Although this format makes scanf
read the number and store it into i
, this "reading" continues and it lasts till non-whitespace character followed by
is found. This is the reason why input 1 2
makes this scanf
stop.
在这种情况下,您不应在输入格式中指定换行符.请改用 scanf("%d",&i);
.
You should not specify newline in the input format in this case.
Use scanf("%d",&i);
instead.
这篇关于scanf("%d ",&i) 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:scanf("%d ",&i) 的问题


- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 近似搜索的工作原理 2021-01-01