c++ - 格式化 C++ 输出两列不同的理由

标签 c++ formatting output cout

这是我的代码:

cout << fixed <<showpoint<<setprecision(2);
cout << setw(10) << left << "Employee:"<< right << setw(9) << name <<endl;
cout << setw(10) << left << "Hours Worked:"<< right << setw(9) << hrWork <<endl; 
cout << setw(10) << left << "Hourly Rate:"<< right << setw(9) << hrRate <<endl;
cout << setw(10) << left  << "Total Wages:"<< right << setw(9) << tWages <<endl << endl;

cout << left << setw(10) << "Federal Withholding:"<< right << setw(9) << fedW <<endl;
cout << left << setw(10) << "State Withholding:"<< right << setw(9) << staW <<endl;
cout << left << setw(10) << "Hospitalization:"<< right << setw(9) << hosp <<endl;
cout << left << setw(10) << "Union Dues:"<< right << setw(9) << uniDues <<endl;
cout << left << setw(10) << "Total Deductions:"<< right << setw(9) << tDeduc <<endl;
cout << left << setw(10) << "Net Pay:"<< right << setw(9) << netP <<endl;

这是我当前的输出(我尝试了 set(w) 和几种方法的证明。这是我能得到的最接近的结果):

Employee:   Anthony
Hours Worked:    20.00
Hourly Rate:     8.75
Total Wages:   175.00

Federal Withholding:    31.50
State Withholding:     7.88
Hospitalization:    25.65
Union Dues:     3.50
Total Deductions:    68.53
Net Pay:     106.47

这就是我的目标(两列,一列向左对齐,另一列向右对齐):

Employee:             Anthony
Hours Worked:           20.00
Hourly Rate:             8.75
Total Wages:           175.00

Federal Withholding:    31.50
State Withholding:       7.88
Hospitalization:        25.65
Union Dues:              3.50
Total Deductions:       68.53
Net Pay:               106.47

我该怎么做?谢谢

最佳答案

左边的大多数字符串都超过 10 个字符,使用例如 setw(25)。

关于c++ - 格式化 C++ 输出两列不同的理由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515738/

相关文章:

c++ - 使cmake将命令行变量传递给编译器

c++ - C++中哪种数据结构适合实现浏览器历史记录?

c# - 在 MVC5 中使用 Html.DisplayFor() 的日期格式

C 字数统计直方图应用程序不返回输出

C++:为什么 cout 打印回车符以及使用 ifstream 从文件读取的字符串?

c++ - 由于#define,VS2015 找不到成员函数 [c++] 定义

c++ - 为什么 In-place QuickSort 比 C++ 中的普通版本慢?

replace - 替换 Rust 中的路径部分

git pre-commit hook 同时格式化和重新添加文件

java - 将 shell 输出重定向到文件