2014-09-18 572 views
0

我想用C++來輸出一個類似輸出的表。它應該是這樣的如何用cout設置固定寬度?

Passes in Stock : Student Adult 
------------------------------- 
Spadina    100 200 
Bathurst    200 300 
Keele     100 100 
Bay     200 200 

但我的總是看起來像

Passes in Stock : Student Adult 
------------------------------- 
Spadina    100 200 
Bathurst    200 300 
Keele    100 100 
Bay    200 200 

我的代碼的輸出

std::cout << "Passes in Stock : Student Adult" << std::endl; 
std::cout << "-------------------------------"; 

    for (int i = 0; i < numStations; i++) { 

     std::cout << std::left << station[i].name; 
     std::cout << std::right << std::setw(18) << station[i].student << std::setw(6) << station[i].adult << std::endl; 

    } 

我如何改變它,所以它看起來像在頂部的輸出?

+3

如果第一和第二列沒有這樣做,那麼設置第二列和第三列的固定值將不會很有用。 – hvd 2014-09-18 16:47:07

回答