iomanip有问题,专栏没有按我预期的方式排列
Having trouble with iomanip, columns not lining up the way I expect
完成一个长项目,最后一步是确保我的数据排在正确的列中。容易的只是我在这方面遇到了麻烦,而且我做这件事的时间比我想承认的要长,我看了很多视频,真的不知道该怎么办。所以这里有一小段我遇到麻烦的代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
cout << "Student Grade Summaryn";
cout << "---------------------nn";
cout << "BIOLOGY CLASSnn";
cout << "Student Final Final Lettern";
cout << "Name Exam Avg Graden";
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条目的数字都歪斜了。我以为赛斯应该允许你忽略这一点?我错过了什么?感谢
它并不像你想象的那样工作。std::setw
仅为下一次插入设置字段的宽度(即,它不是"粘性")。
试试这样的东西:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "Student Grade Summaryn";
cout << "---------------------nn";
cout << "BIOLOGY CLASSnn";
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';setw()怎么了?
操纵器<< right << setw(23)
正在告诉ostream
您想要字符串"89"设置在23个字符宽的字段的右侧边缘。没有什么可以告诉ostream您希望该字段从哪里开始,但是,除了由于最后一行换行符。并且<< "bill"<< " " << "joeyyyyyyy"
向输出写入更多的字符与<< "Bob James"
相比,第二行的23个字符宽的字段在第一行的同一个字段的左边开始。
流操纵器会影响流式处理的下一个输入/输出值,然后一些操纵器(包括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 Summaryn";
cout << "---------------------nn";
cout << "BIOLOGY CLASSnn";
cout << "Student Final Final Lettern";
cout << "Name Exam Avg Graden";
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
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 如何在c++中为模板函数实例创建快捷方式
- 为什么不;名字在地图上是按顺序排列的吗
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 在c代码之间共享数据的最佳方式
- 在C++中将函数压缩为两种方式
- 按对象的特定方法按升序排列的C++优先级队列
- 以螺旋方式打印矩阵的程序.(工作不好)
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 创建引用向量的优雅方式
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 不同/较旧的处理器运行c++代码的方式是否不同
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 像素在 SDL2 中的排列方式
- C++结构来定义数据的排列和访问方式
- iomanip有问题,专栏没有按我预期的方式排列
- LLVM clang编译器优化器以一种非常奇怪的方式重新排列代码,该怎么做