1

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

 #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 应该允许你忽略它?我错过了什么?谢谢

4

3 回答 3

2

它不像你想的那样工作。std::setw仅为下一次插入设置字段的宽度(即,它不是 "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";
}

还相关:setw() 有什么用?

于 2015-04-07T22:49:54.117 回答
1

操纵<< right << setw(23)器告诉ostream您希望将字符串“89”设置在 23 个字符宽的字段的右侧边缘。但是,没有什么可以告诉 ostream 您希望该字段从哪里开始,除了自最后一个换行符以来输出的字符串的宽度。并且<< "bill"<< " " << "joeyyyyyyy"在输出中写入的字符比实际要多得多<< "Bob James",因此第二行的 23 个字符宽的字段开始于第一行同一字段的左侧相当多的位置。

于 2015-04-07T22:50:02.133 回答
1

流操纵器会影响下一个流式传输的输入/输出值,然后一些操纵器(包括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
于 2015-04-07T23:14:25.197 回答