【发布时间】:2019-04-01 23:08:25
【问题描述】:
我正在完成我正在处理的这个 c++ 项目,以获取用户输入、计算一些值,然后将所有数据写入文件。我的问题是我无法在文本文件中正确对齐值。我正在使用 setw(),但是当用户输入的长度未知时,这并不能正确对齐所有内容。它只是弄乱了列,使它们不对齐。
我尝试过使用固定运算符,左对齐,右对齐,但运气不佳
这是我关于写入文件的代码。
if (myfile.is_open()){
myfile << "BASKETBALL COURTS AREA REPORT\n\n";
myfile << "Court" << setw(25) << "Height" << setw(25) << "Width\n";
for(int i=0; i<n; i++){
myfile << names[i] << setw(25) << " " << arr1[i] << setw(25) << arr2[i] <<"\n\n";
}
}
myfile << "\nThe largest court is " << maxName << ": " << maximum << "\n" << "\n";
myfile << "Total area covered by all courts: " << totalArea;
I expect the columns to be completely aligned like in this picture:
However the actual output looks more like this:
如果有人能帮助我做什么,我将不胜感激。非常感谢您的宝贵时间!
【问题讨论】: