【问题标题】:What to do to see the output of "cout" command?如何查看“cout”命令的输出?
【发布时间】:2015-12-04 13:39:25
【问题描述】:

我从 C++(Visual Studio 2015 和 Windows 8.1)开始,使用这个简单的代码:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world" << endl;
    return 0;
}

但是,输出屏幕什么都没有显示!我该怎么办?

提前致谢。

【问题讨论】:

  • 你是如何启动这个程序的?如果你只是编译并双击它,它会打印然后立即退出,肉眼看不到。尝试从命令行启动程序。
  • 如果您从 Visual Studio 运行,那么它将启动应用程序并将文本输出到刚刚打开的 控制台窗口,然后程序将退出。您可以在return 0; 上设置断点,然后您可以切换到控制台并查看输出。您还可以打开命令提示符并从那里运行程序。
  • 您是否在提示符下检查了 .exe,而没有从 ide 传递?尝试从控制台 (win+x) cmd 运行您的 rprogram,然后进入项目的目录输出
  • 使用conio.h中定义的getch函数
  • 我认为学习基本的MS Visual Studio调试会很有帮助。就像在要分析的行处设置断点,然后使用 F5 到达该点并推理您的输出直到该点。此外,我认为在您完成代码之前,请始终尝试在调试模式下工作,即使用 F5 启动程序并习惯放置断点。

标签: c++ output cout


【解决方案1】:

在 Visual Studio 中,使用 Ctrl-F5 启动程序,它会自动为您运行和暂停。不需要额外的代码。

【讨论】:

    【解决方案2】:

    您的代码非常好,但程序目前只打印并在之后立即退出,因为这可能发生得非常快,您甚至可能看不到它,请尝试暂停它:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Hello world" << endl;
        cin.get();
        return 0;
    }
    

    另外,请确保您的防病毒软件没有阻止 Visual Studio。

    【讨论】:

    • 谢谢,那只是因为我的杀毒软件!但这是为什么呢?
    • @Nima 因为某些防病毒软件过于敏感。
    • 工作正常,但是当我将“Hello world”更改为其他内容时,它会继续打印旧内容!有错误。我该怎么办?
    【解决方案3】:

    您的代码很好,但是,如果您将其作为 cmd 程序执行,程序窗口将立即关闭,您甚至可能看不到输出。您可以通过“暂停”程序来编写额外的代码来解决这个问题:

    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
        cout << "Hello world" << endl;
        system("PAUSE");
        return 0;
    }
    

    如果您不喜欢每次键入时都包含 windows.h 文件,您可以添加“cin.get();”在代码的末尾。但老实说,既然你只是一个初学者,我认为你应该尝试的最酷的方法,不是使用 Visual Studio 来学习 C/C++,而是安装 CodeBlocks(一个简单但有效的 IDE)来编写一些不熟悉的代码这么久。要知道,VS 是针对大型复杂项目和一些实用程序开发的。

    【讨论】:

      【解决方案4】:

      另一种解决方案,取决于平台。我的答案适用于那些只需要测试暂停以进行调试的人。不推荐发布解决方案!

      窗口

      #include <iostream>
      
      int main()
      {
          std::cout << "Hello world" << endl;
          system("pause");
          return 0;
      }
      

      linux (and many alternatives)

      #include <iostream>
      
      int main()
      {
          std::cout << "Hello world" << endl;
          system("read -rsp $'Press enter to continue...\n'");
          return 0;
      }
      

      检测平台

      我曾经在编写家庭作业时这样做,确保这只发生在 Windows 上:

      #include <iostream>
      int main()
      {
          std::cout << "Hello world" << endl;
          #ifdef _WIN32
              system("pause");
          return 0;
      }
      

      这是ifdef 宏和操作系统的一个很好的备忘单:http://sourceforge.net/p/predef/wiki/OperatingSystems/

      【讨论】:

        【解决方案5】:

        程序在return 0; 上退出并关闭窗口。在此之前,您必须暂停程序。例如,您可以等待输入。

        这是我的代码中的一个 sn-p 来执行此操作。它适用于 windows 和 linux。

        #include <iostream>
        
        using std::cout;
        using std::cin;
        
        // Clear and pause methods
        #ifdef _WIN32
        // For windows
        void waitForAnyKey() {
            system("pause");
        }
        
        #elif __linux__
        // For linux
        void waitForAnyKey() {
            cout << "Press any key to continue...";
            system("read -s -N 1"); // Continues when pressed a key like windows
        }
        
        #endif
        
        int main() {
            cout << "Hello World!\n";
            waitForAnyKey();
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2021-12-03
          • 1970-01-01
          • 1970-01-01
          • 2011-01-10
          • 1970-01-01
          • 2017-11-20
          • 2019-09-09
          相关资源
          最近更新 更多