多列中的C++对齐方式

C++ justification in multiple columns

本文关键字:对齐 方式 C++      更新时间:2023-10-16

我正试图编写一个程序来显示每个月的帐户余额,但在获得3列以正确证明时遇到了问题。预期的输出应该看起来像:

                 Total  
Month            Accumulated  
------           -----------  
2015 March            500.00  
2015 April           1001.13  
2015 May             1503.38  
2015 June            2006.76  
2015 July            2511.28

但我的输出是这样的:

                 Total  
Month            Accumulated  
------           -----------  
2015 March            500.00  
2015 April            1001.13  
2015 May              1503.38  
2015 June             2006.76  
2015 July             2511.28

我需要让数字在右边均匀排列。我似乎无法用正确的理由让它看起来更接近我需要的东西。我希望这里有人可能有一个我不知怎么忽略的简单解决方案。目前,这行是这样写的:

cout << setw(5) << left << currentYear << setw(18) << currentMonthName << totalAccum << endl;

如有任何建议,我们将不胜感激。谢谢

我想好了-需要添加一个"作为占位符,然后进行正确的对齐:
cout << setw(5) << left << currentYear << setw(10) << currentMonthName << " " << right << setw(13) << totalAccum << endl;