【问题标题】:Having trouble with iomanip, columns not lining up the way I expectiomanip 遇到问题,列未按我预期的方式排列
【发布时间】:2015-04-07 22:40:51
【问题描述】:

完成一个漫长的项目,最后一步是确保我的数据在正确的列中排列。简单。只有我在这方面遇到了麻烦,而且我在这方面的时间比我愿意承认的观看许多视频的时间更长,并且无法真正掌握到底要做什么所以这里有一些我遇到问题的代码的 sn-p与:

 #include <iostream>
 #include <iomanip>   


 using namespace std;

 int main(){        

    cout << "Student Grade Summary\n";
    cout << "---------------------\n\n";
    cout << "BIOLOGY CLASS\n\n";
    cout << "Student                                   Final   Final Letter\n";
    cout << "Name                                      Exam    Avg   Grade\n";
    cout << "----------------------------------------------------------------\n";
    cout << "bill"<< " " << "joeyyyyyyy" << right << setw(23) 
         << "89" << "      " << "21.00" << "   "
         << "43" << "\n";
    cout << "Bob James" << right << setw(23)  
         << "89" << "      " << "21.00" << "   "
         << "43" << "\n";
    }

适用于第一个条目,但 bob james 条目的数字全部歪斜。我以为 setw 应该允许你忽略它?我错过了什么? 谢谢

【问题讨论】:

    标签: c++ cout iomanip setw


    【解决方案1】:

    它不像你想象的那样工作。 std::setw 仅为下一次插入设置字段的宽度(即it is not "sticky")。

    试试这样的:

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main() {
    
        cout << "Student Grade Summary\n";
        cout << "---------------------\n\n";
        cout << "BIOLOGY CLASS\n\n";
    
        cout << left << setw(42) << "Student" // left is a sticky manipulator 
             << setw(8) << "Final" << setw(6) << "Final"
             << "Letter" << "\n";
        cout << setw(42) << "Name"
             << setw(8) << "Exam" << setw(6) << "Avg"
             << "Grade" << "\n";
        cout << setw(62) << setfill('-') << "";
        cout << setfill(' ') << "\n";
        cout << setw(42) << "bill joeyyyyyyy"
             << setw(8) << "89" << setw(6) << "21.00"
             << "43" << "\n";
        cout << setw(42) << "Bob James"
             << setw(8) << "89" << setw(6) << "21.00"
             << "43" << "\n";
    }
    

    还相关:What's the deal with setw()?

    【讨论】:

    • aww 这在名称是一个字符串时有效,但如果两个字符串分开则无效。在我的实际程序中,我必须获取这些值,然后将它们抽出,我想我可以创建一个新字符串来保存这两个值。有什么方法可以确保 '
    • 最好把名字串联起来,否则很难猜出setw应该放什么,因为这取决于名字的长度。如果你的名字是std::string(你应该避免char*),那么很容易,在显示时使用+运算符将它们连接起来,比如cout &lt;&lt; s1 + s2
    • 好的是的,我之前就用过那个方法,但它把姓氏扔得很远,所以我没有再试一次,但谢谢你的帮助!我知道现在该做什么了!
    • @user3470987 查看更新后的编辑,一旦您知道所需的字段宽度,就可以在之后使用它们
    • setw 使用起来比硬编码空格键更智能/更快捷吗?
    【解决方案2】:

    操纵者&lt;&lt; right &lt;&lt; setw(23) 告诉ostream 你想要 字符串“89”设置在 23 个字符宽的字段的右侧边缘。 没有什么可以告诉 ostream 您希望该字段在哪里开始, 然而,除了输出的字符串的宽度,因为 最后一个换行符。 &lt;&lt; "bill"&lt;&lt; " " &lt;&lt; "joeyyyyyyy" 在输出中写入更多字符 比 &lt;&lt; "Bob James" 更重要,所以第二行的 23 个字符宽的字段 开始于第一行同一字段的左侧相当多的位置。

    【讨论】:

    • 那么我如何确保无论名称的长度如何,89 都会显示在需要的位置?这是在期末考试现场。请记住,名称在我的实际程序中会发生变化,我不能只是硬编码其中的 setw
    • 在输出需要约束/对齐的文本之前设置宽度和对齐方式。你是在事后才这样做,这为时已晚。
    • 通常当我想使用 setw 在列中排列内容时,我会在写入流的每个字符串或数字之前插入一个 setw(除了在输出,因为文字字符串的名称定义了它将显示的宽度)。
    【解决方案3】:

    流操纵器会影响下一个流式传输的输入/输出值,然后一些操纵器(包括setw())会在之后重置。因此,您需要在输出文本字符串之前设置宽度和对齐方式,而不是之后。

    试试这样的:

    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    void outputStudent(const string &firstName, const string &lastName,
        int finalExam, float finalAvg, int letterGrade)
    {
        cout << setw(40) << left  << (firstName + " " + lastName) << " "
             << setw(6)  << right << finalExam << " "
             << setw(6)  << right << fixed << setprecision(2) << finalAvg << " "
             << setw(7)  << right << letterGrade << "\n";
    }
    
    int main()
    {
        cout << "Student Grade Summary\n";
        cout << "---------------------\n\n";
        cout << "BIOLOGY CLASS\n\n";
        cout << "Student                                   Final  Final  Letter\n";
        cout << "Name                                      Exam   Avg    Grade\n";
        cout << "--------------------------------------------------------------\n";
    
        outputStudent("bill", "joeyyyyyyy", 89, 21.00, 43);
        outputStudent("Bob", "James", 89, 21.00, 43);
    
        cin.get();
        return 0;
    }
    

    输出:

    Student Grade Summary
    ---------------------
    
    BIOLOGY CLASS
    
    Student                                   Final  Final  Letter
    Name                                      Exam   Avg    Grade
    --------------------------------------------------------------
    bill joeyyyyyyy                              89  21.00      43
    Bob James                                    89  21.00      43
    

    【讨论】:

      猜你喜欢
      • 2019-02-15
      • 2019-12-12
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多