【发布时间】:2017-05-15 15:47:45
【问题描述】:
我能够对齐列,但我的编码出现问题,我的“%”无法连接到百分比变量。
这是我的输出/对齐编码:
//HEADER
outFile << left << setfill(' ') << setw(15) << "Member Name"
<< setfill(' ') << setw(20) << "Portion Achieved"
<< setfill(' ') << setw(12) << "Grade (%)"
<< setfill(' ') << setw(15) << "Letter Grade"
<< setfill(' ') << setw(15) << "Comment" << endl;
while (count <= 6)
{
cout << "Enter Name: ";
cin >> student.name;
cout << "Points Achieved: ";
cin >> student.points;
cout << "\n";
decimal = (student.points/60);
percent = decimal*100;
outFile << left << setfill(' ') << setw(15) << student.name
<< setfill(' ') << setw(20) << decimal
<< setfill(' ') << setw(12) << percent << " %"
<< setfill (' ') << setw(15);
这是输出的样子:
Member Name Portion Achieved Grade (%) Letter Grade Comment
Min 0.166667 16 %F Sorry, you did not pass :(
Carmela 0.333333 33 %F Sorry, you did not pass :(
Jayson 0.5 50 %F Sorry, you did not pass :(
Kristin 0.666667 66 %D Needs Improvement!
Mae 0.833333 83 %B Well Done!
JT 1 100 %A Excellent!
VS 我希望它看起来如何:
Member Name Portion Achieved Grade (%) Letter Grade Comment
Min 0.166667 16% F Sorry, you did not pass :(
Carmela 0.333333 33% F Sorry, you did not pass :(
Jayson 0.5 50% F Sorry, you did not pass :(
Kristin 0.666667 66% D Needs Improvement!
Mae 0.833333 83% B Well Done!
JT 1 100% A Excellent!
【问题讨论】:
-
试试
<< percent << setw(12) << " %"而不是<< setw(12) << percent << " %" -
@tobi303:那行不通。
-
它使 # 和 % 保持不变,但现在我的最后两列未对齐。
-
无论百分比值有多长,Tobi 的建议都是在
" %"周围添加十个空格。