我是 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
与编程和 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
即可在命令行上构建应用程序