简体   繁体   English

在简单的C ++程序中没有控制台输出

[英]No console output in simple c++ program

I noticed a weird behaviour of some of my programs in c++ and when I was trying to figure out what coused it, I found out, that something wrong is going on with my console outputs. 我注意到我的某些程序在c ++中的行为很奇怪,当我试图弄清楚是什么原因时,我发现控制台输出出现了问题。 I used iostream and cstdio functions with the same behaviour. 我使用iostream和cstdio函数具有相同的行为。 When I print something on console It doesn't display at all. 当我在控制台上打印某些内容时,它根本不会显示。 Here are codes that I used for observing this strange behaviour. 这是我用来观察这种奇怪行为的代码。 This piece of code outputs everything propertly (even if it shouldn't IMO): 这段代码正确地输出了所有内容(即使它不应该是IMO):

#include <cstdio>
using namespace std;
int main(void) {
    int a = 0;
    scanf("%d", &a);
    a++;
    printf("result is %d", a);
}

This one however(correct I think) doesn't display anything, only the run finished message 然而,这(我认为是正确的)不显示任何内容,仅显示运行完成的消息

#include <cstdio>
using namespace std;
int main(void) {
    int a = 0;
    scanf("%d", &a);
    a++;
    printf("result is %d \n", a);
}

I also tried it with removed space before "\\n" with no difference. 我也尝试过删除“ \\ n”之前的空格,没有区别。 However, when I place more the same printf functions with "\\n" at the end to the program, everything displays correctly (multiple times of course). 但是,当我在程序的末尾放置更多相同的printf函数时,它们以“ \\ n”结尾时,一切都会正确显示(当然是多次)。 Iostream behaves in similar way - using endl doesn' t cause anything to appear on console. Iostream的行为类似-使用endl不会在控制台上显示任何内容。 What am I doing wrong? 我究竟做错了什么? As to the original code that caused malfunctioning, I noticed that on my output nothing appeared but in my school, the same code output everything correctly. 至于引起故障的原始代码,我注意到输出上什么也没出现,但在我的学校中,相同的代码正确输出了所有内容。 I am working under NetBeans 8.0.2. 我正在NetBeans 8.0.2下工作。 Thanks in advance for help 预先感谢您的帮助

All output data is buffered, before it getting printed to console. 在将所有输出数据打印到控制台之前,将对其进行缓冲。 You can use fflush or \\n to flush the output stream and print all data. 您可以使用fflush\\n刷新输出流并打印所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM