我能够对齐列,但是我的编码遇到麻烦,我的“%”将不会连接到percent变量。

这是我对输出/对齐的编码:

//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值的12个字符的“宽度”。事后发生,事后发生。这里没有代码可以将任何东西“连接”在一起,C ++流中也没有任何这样的功能。

您可能必须将percent + " %"(伪代码)预先构造为字符串,然后使用该单个字符串作为列值。也许:

<< setfill(' ') << setw(12) << (std::to_string(percent) + " %")


否则,请删除setw并自己编写n个空格。为此,您需要确定字符串化的percent文本的长度,加2(表示“%”),然后减去12。将std::string(n, ' ')写入流中。 !

关于c++ - 列对齐问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43983784/

10-11 22:43