2016-01-11 19 views
3

在該代碼中,我想在從0開始的特殊格式打印編號,以1000固定文本前述格式,是這樣的:C++輸出使用運輸及工務局局長和setfill

測試001
測試002
測試003
...
測試999

但是,我不喜歡它顯示爲

測試1
測試2
...
測試10
...
測試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; 
} 
+0

我認爲你需要把'setw'和'setfill'只是beofre'i':'性病::法院<<的std :: setfill(0)<<的std ::運輸及工務局局長(3) 「我;'。 –

回答

9

setfill的和setwmanipulators是用於輸出運轉。所以在你的情況下,你將其設置爲TEXT的輸出。

取而代之,例如,

oFile << TEXT << std::setfill('0') << std::setw(3) << i << endl; 
相關問題