2014-09-18 574 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

回答