c++控制台输出显示在屏幕上

标签 c++ visual-studio-2010 cout

<分区>

我是 C++ 的新手。我正在使用 visual studio 2010 专业版。我尝试运行此代码并且运行良好,但问题在于显示大量输出。

int main(){
    for(int i=0;i<=10000;i++) 
       print<<i;
    return 0;
}

当我尝试输出从 1 到 10,000 的数字时,控制台上只显示最后 500-1000 个数字。如何查看屏幕上的所有数字?如果我减少循环执行的次数,它会显示所有数字。

我可以使用控制台解决这个问题还是必须采用其他方法?

最佳答案

您将需要增加控制台缓冲区的大小,但这并不是 SO 的真正主题。您可以在 here 中找到对此的详尽解释。虽然。

为方便起见,以下是内嵌的步骤:

1) Click on Start > Run > cmd

2) Right click on the command prompt window > Properties

3) In the "Option" tab, modify the value next to the "Buffer size" entry

buffer size setting

与编程和 SO 主题最接近的是使用分隔符,如“,”,而不是换行或写入文件。

','分隔符

#include <iostream>
using namespace std;

int main(){
    int i = 0;
    while (i < 10000) 
        cout << i++ << ", ";
    cout << i;
    return 0;
}

写入文件

#include <iostream>
#include <fstream>
using namespace std;

int main () {
    ofstream myfile;
    myfile.open("example.txt");

    for (int i = 0; i < 10000; ++i)
        myfile << i << endl;

    myfile.close();
    return 0;
}

然后,您只需运行 cl.exe main.cpp 即可在命令行上构建应用程序

关于c++控制台输出显示在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18671053/

相关文章:

c++ - 在 Windows 中获取安装前缀的最佳方法是什么

c++ - 子字符串或在 C++ 中将数组打印到 num of position

c++ - 在 C++Amp 中减少 GPU-CPU 数据传输

c++ - AMQP-CPP RabbitMQ 构建集成到 CPP 项目

c++ - 变量不是类型名称错误

C++ 初学者输入错误数据类型时的无限循环并帮助评估我的代码

c# - 更改 Outlook 收件箱图标信封图标

vb.net - 为表单创建分部类

c++ - cout什么时候刷新?

c++ - g++ 编译器为表达式提供 << 类型错误,但在 Visual Studio 中有效