2015-11-09 46 views
0

所以我使用的iomanip库来清点这样的:对齐问题<iomanip>

std::cout << std::endl 
    << std::left << std::setw(15) << "Ticker Symbol" 
    << std::setw(100) << "Stock Name" 
    << std::setw(12) << "Value" 
    << std::setw(10) << "Date" 
    << std::setw(10) << "YTD Return" 
    << std::endl; 

的问题是,它结束了印刷本:

T  icker SymbolS      tock NameV  alueD  ateY TD Return 

相反的:

Ticker Symbol Stock Name       Value Date YTD Return 

有没有一种方法可以解决这个问题,而无需使用其他库?

编辑:我的操作符重载函数似乎是造成这个问题:

std::ostream& operator<< (std::ostream& out, const char array[]) 
{ 
    for (uint8_t i = 0; array[i] != '\0'; i++) 
    { 
     out << array[i]; 
    } 
    return out; 
} 

话虽这么说,我还是不知道如何解决这个问题。

+0

在黑暗中拍摄:在没有写之间帮助刷新流? –

+0

把'std :: cout.flush();'放在代码之前什么都不做。 – LarryK

+2

[Can not Reproduce](http://coliru.stacked-crooked.com/a/9f71dfe3af6aa51f) – NathanOliver

回答

0

为什么你必须超载操作员?这当然是造成你的问题。

setw将得到应用到输出第一件事就是到流,因为你同时拥有C字符串输出一个字母,将setw被应用到只有首字母 - 制作你所看到的行为。

最简单的事情就是摆脱运营商的过载。否则,你需要从cout.width()宽度,输出一个字母的时间,那么多余的空格后增加:

std::ostream& operator<< (std::ostream& out, const char array[]) 
{ 
    int width = out.width(); 
    out.width(0); 
    int array_size = 0; 
    for (uint8_t i = 0; array[i] != '\0'; i++, array_size++) 
    { 
     out << array[i]; 
    } 

    for (int i = array_size; i < width; i++) 
    { 
     out << " "; 
    } 

    return out; 
}