- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
C++ 代码运行良好,但当前向右输出值但向左对齐且未在小数点上对齐。不能使用 put_money,我错过了什么?
尝试使用 fprint 和 put_money,与同学确认我们应该使用 setw(x)。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const double taxRate = 0.09;
const double laborCPH = 35.0; //where CPH is Cost Per Hour
double costParts;
double costLabor;
double totalTax;
double totalDue;
string name;
double laborHours;
cout << "What is your name? ";
cin >> name;
cout << "How many hours of labor needed? ";
cin >> laborHours;
costLabor = laborHours * laborCPH;
cout << "What was the cost of the parts and supplies? ";
cin >> costParts;
cout << endl << endl;
totalTax = costParts * taxRate;
totalDue = costParts + totalTax + costLabor;
cout.precision(2);
cout << setw(25) << left << "Customer Name " << fixed << right << internal << name << endl;
cout << setw(25) << left << "Hours of Labor " << fixed << right << internal << laborHours << endl;
cout << setw(25) << left << "Cost for Labor " << fixed << right << internal << costLabor << endl;
cout << setw(25) << left << "Parts and Supplies " << fixed << right << internal << costParts << endl;
cout << setw(25) << left << "Tax " << fixed << right << internal << totalTax << endl;
cout << setw(25) << left << "Total Amount Due " << fixed << right << internal << totalDue << endl;
return 0;
}
实际输出:
What is your name? Jones
How many hours of labor needed? 4.5
What was the cost of the parts and supplies? 97
Customer Name Jones
Hours of Labor 4.50
Cost for Labor 157.50
Parts and Supplies 97.00
Tax 8.73
Total Amount Due 263.23
期望的输出:
What is your name? Jones
How many hours of labor needed? 4.5
What was the cost of the parts and supplies? 97
Customer Name Jones
Hours of Labor 4.50
Cost for Labor 157.50
Parts and Supplies 97.00
Tax 8.73
Total Amount Due 263.23
最佳答案
当您在打印前使用 std::internal
填充剩余空间时,它将打印所有 剩余空间到终端。如果仔细查看输出,您会发现在数字开头之前有 25 个字符(如果是第一行,则为您的姓名。)
您可以通过计算要打印的字符串的长度并从 setw()
调用中减去该长度来解决这个问题,但是对于固定精度的 float 和 double 来说,这会变得很复杂。幸运的是,有一种更简单的方法!
正如 Fred Larson 在评论中指出的那样,解决此问题的一个好方法是在第一次调用后添加第二个 setw()
和 std::internal
到 std::internal
像这样:
cout.precision(2);
cout << setw(20) << left << "Customer Name " << fixed << right << internal << setw(7) << internal << name << endl;
cout << setw(20) << left << "Hours of Labor " << fixed << right << internal << setw(7) << internal << laborHours << endl;
cout << setw(20) << left << "Cost for Labor " << fixed << right << internal << setw(7) << internal << costLabor << endl;
cout << setw(20) << left << "Parts and Supplies " << fixed << right << internal << setw(7) << internal << costParts << endl;
cout << setw(20) << left << "Tax " << fixed << right << internal << setw(7) << internal << totalTax << endl;
cout << setw(20) << left << "Total Amount Due " << fixed << right << internal << setw(7) << internal << totalDue << endl;
这允许终端用空格填充输出,然后用额外的空格缓冲您的数字或字符串,最后打印所有对齐的数字。只要确保为您的第二个 setw()
提供足够的空间来容纳大量数字,如果您期望它们的话。
关于使用 setw(x) 而不是 put_money 对齐小数点的 C++ cout 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57828082/
我想在 C++ 上显示一个“漂亮”的国家列表及其 ISO 货币代码。 问题是我的数据是法语,并且有重音字符。这意味着 Algolia ,实际上写成“Algérie”,而瑞典变成“Suède”。 map
我需要在控制台上打印一些数据。我的代码是: cout << setw(5) << left << "id" << " | " << setw(10) << left << "computer" <<
我想要一个 setw 接受两个参数并返回最大的一个。这可能吗?我该怎么做呢?不寻找代码只是一些方向会很好,因为我无法在线找到明确的答案。 最佳答案 我不认为你可以重载它,但你当然可以用不同的名称定义你
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我有一个名为 os 的 ostream,我这样使用: os << rec.lastname << " " << rec.firstname << setw(30) << rec.phonenum; 输
我正在尝试使用 NTL 库(一个数论库)来格式化输出。其中一个对象是 GF2X 对象,它是一个表示为一串系数的多项式。一个简单的例子: GF2X a = GF2X(5,1); a++; cout(GF
我的代码有问题,不确定是错误还是我的代码有问题。 #include #include using namespace std; int main() { cout << setfill('*')
我正在尝试使用 setw 来清理程序的输出。我想要“要订购的线轴总数”和输出之间的空格。 编辑这就是我想要的: 这就是我得到的 这是我到目前为止所拥有的:更新代码 /*****************
我正在尝试解析一个文本文件,并使用 setw() 将内容输出到控制台并进行格式化。我的问题是只有第一行的格式正确,其余行默认返回到左侧。 while (test) { cout > price
我写了这样一段代码: int d{ 0 }; cin >> setw(2) >> d; 但似乎setw()对读取整数没有影响。如果是这样,我们如何使用 istream 实现 scanf() 的 %2d
我正在尝试以 hh:mm 格式打印时间,但当时间类似于 01:01 时,它打印为 1:1。这是我的代码: void output(int hour, int min, char ampm){
我想像这样证明输出文本 0x29823d80 0x10019b8 / 0 00000000000000000000000000000001 0x37449e60
#include #include #include using namespace std; ifstream f("atestat.in"); ofstream g("atestat.out");
有没有办法强制setw截断? 假设我想得到输出: blah blah blee le bu blah blah blee 有没有办法让它工作: string foo{"bu blah blah ble
这是我的功能 ostream margain(std::string firstWord) { ostream x; x << std::setw(20) << firstWord;
我正在处理格式,但我没有得到正确的格式。 这是我的代码,这是我得到的输出结果 cout << fixed << setprecision(2) << setw(3) << right << sourc
应该是一个微不足道的问题,但是发现setw只适用于它紧接的后续输出,并不确定如何让它适用于所有后续输出。 例如,对于下面这行代码 cout<
我正在尝试使用设置宽度 setw 将字符串输出到输出文件,但是,我无法使其工作。我有以下示例。 // setw example #include #include #includ
我有这个示例代码块: #include #include #include using namespace std; int main(){ string blank = " ";
在c++中,setw函数用于设置下一次插入操作要使用的字符数作为字段宽度。 C 中是否有任何函数,我的意思是,在标准 C 库中,它做同样的事情? 最佳答案 printf("%5d", 42); 将使用
我是一名优秀的程序员,十分优秀!