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) 的问题
基础教程推荐
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- c++ STL设置差异 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
