在这个代码中,我希望从0到1000以特定格式打印数字,并在其前添加固定文本,如下所示:
Test 001 Test 002 Test 003 ... Test 999
但是,我不想将其显示为:
Test 1 Test 2 ... Test 10 ... Test 999
以下的 C++ 程序有什么问题使它无法执行上述任务?
Test 001 Test 002 Test 003 ... Test 999
但是,我不想将其显示为:
Test 1 Test 2 ... Test 10 ... Test 999
以下的 C++ 程序有什么问题使它无法执行上述任务?
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
const string TEXT = "Test: ";
int main()
{
const int MAX = 1000;
ofstream oFile;
oFile.open("output.txt");
for (int i = 0; i < MAX; i++) {
oFile << std::setfill('0')<< std::setw(3) ;
oFile << TEXT << i << endl;
}
return 0;
}
i
前面加上setw
和setfill
:std::cout << std::setfill('0') << std::setw(3) << i;
。 - triple_r