【问题标题】:std::cout inside main() not printing anything in debug console during debuggingmain() 中的 std::cout 在调试期间不在调试控制台中打印任何内容
【发布时间】:2020-10-07 02:22:55
【问题描述】:

主函数内的任何 cout 语句在调试期间不打印任何值,但其他函数内的 cout 在函数调用期间打印值(我使用 vscode)

#include<iostream>
using namespace std;
int main()
{
int a;
a=9;
if(a==9)
{
cout<<"hello";}
return 0;
}

当它被调试时,在 main() 的第一行放置一个断点 调试控制台中不打印“hello”。

#include <iostream>
using namespace std;
void fun(int n)
{
if (n > 0)
{
    cout << n << endl;
    fun(n - 1);


}
}

int main()
{
int x = 3;
fun(3);
cout<<x;
return 0;
}

但是当通过在 main() 的第一行放置断点来调试时 值打印为 3 2 1

【问题讨论】:

  • “其他功能”的打印效果如何?请花一些时间阅读the help pages,阅读SO tour,阅读How to Ask,以及this question checklist。然后edit你的问题来改进它,例如添加一个minimal reproducible example
  • 如果 main 内部有一个函数调用并且它被调用并且其中存在一个 cout ......那个 cout 正在打印一个值
  • 我怀疑main 中的打印是在函数调用之后并且您的函数永远不会返回。
  • @KaranarjunJr。你能举个例子吗?
  • 请不要描述代码,而是显示给我们。正如我已经提到的,请edit 您的问题包括minimal reproducible example

标签: c++ debugging visual-studio-code


【解决方案1】:

std::cout 被缓冲。尝试使用 std::flush 或 std::endl。

您也可以尝试使用 std::cerr 代替 std::cout。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多