简单的C程序
好的,我正在尝试学习 C,我希望我的用户输入一个值,所以我使用 scanf。我一开始没有冲洗,因为直到我输入两个值之前什么也没有发生。现在我有了它们,尽管我遇到了同样的问题,但在我输入两个数字之前仍然没有输出。这是我的代码:
#include <stdio.h>
using namespace std;
int main()
{
int i1, i2, sums;
printf( "Enter first integer\n" );
fflush(stdout);
scanf( "%d", &i1 );
printf( "Enter second integer\n" );
fflush(stdout);
scanf( "%d", &i2 );
sums = i1 + i2;
printf( "Sum is %d\n", sums );
fflush(stdout);
return 0;
}
任何帮助将不胜感激。
Ok so I am trying to learn C and I want my user to input a value so I am using scanf. I started off not having the flushes, because nothing was comming up until I typed in two values. Now that I have them though I get the same problem there still is no output until after I type in two numbers. Here is my code:
#include <stdio.h>
using namespace std;
int main()
{
int i1, i2, sums;
printf( "Enter first integer\n" );
fflush(stdout);
scanf( "%d", &i1 );
printf( "Enter second integer\n" );
fflush(stdout);
scanf( "%d", &i2 );
sums = i1 + i2;
printf( "Sum is %d\n", sums );
fflush(stdout);
return 0;
}
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下对我来说效果很好:
并给出:
这是在 XP 下使用 Cygwin。您使用什么平台和编译器?
更新:一种可能性是,因为您是在 Eclipse 环境中运行,所以它可能会做一些奇怪的事情,干扰正常的 I/O 规则。
我非常确定,即使 stdout 不像 stderr 那样进行行缓冲,如果您尝试从 stdin 读取(至少在我使用过的大多数环境中,这是少数),它也会自动刷新。
Eclipse 可能正在调整将控制台连接到程序实际 I/O 的方式。我会尝试将代码编译为独立的可执行文件,然后在 Eclipse 环境之外运行它。如果在那里运行正常,那么可能是 Eclipse 和程序之间的交互。
正如我所说,即使没有刷新,您的程序也可以在带有 Cygwin 的 XP 下正常运行。
需要进一步解释。正如 Jerry Coffin 在评论中正确指出的那样,C 标准(c1x,2009/03/01 草案)指出:
可能发生的情况是 Eclipse 与程序输入和输出交互的方式可能导致程序无法将 stdout 识别为交互设备。然后它将被完全缓冲,这意味着在缓冲区已满或程序终止之前您不会看到输出。
The following works fine for me:
and gives:
This is using Cygwin under XP. What platform and compiler are you using?
Update: One possibility is that, because you're running from within the Eclipse environment, it may be doing some weird stuff that interferes with the normal I/O rules.
I'm pretty certain that stdout, even if it's not line buffered like stderr, will autoflush if you attempt to read from stdin (at least in most environments I've used, which is a few).
Eclipse may be fiddling around with the way it attaches the console to the program's actual I/O. I would try to compile the code to a standalone executable and then run it outside the Eclipse environment. If it runs fine there, then it's probably the interaction between Eclipse and the program.
As I stated, your program works fine under XP with Cygwin, even without the flushes.
Further explanation is warranted. As Jerry Coffin rightly points out in a comment, the C standard (c1x, 2009/03/01 draft) states:
What may be happening is that the way Eclipse interacts with the programs input and output may be causing the program to not recognize stdout as an interactive device. It would then be fully buffered, meaning that you wouldn't see the output until the buffer is full, or the program terminates.
我认为你需要在“scanf”旁边添加“\n”。像这样。
尝试一下。
I think you need "\n" in side the "scanf". Like this.
Try it.
我认为你正在冲洗错误的东西,尝试使用 stdin 冲洗 scanf 而不是使用 stdout,就像这样
#include
main()
{
int i、j、总和;
printf("请输入第一个整数\n");
}
i think your are flushing out the wrong stuff , try flushing scanf using stdin not using stdout,just like this
#include
main()
{
int i, j,sums;
printf("enter the first integer\n");
}