【问题标题】:Formatting C++ Output two Columns different Justification格式化 C++ 输出两列不同的对齐方式
【发布时间】:2016-02-19 21:52:06
【问题描述】:

这是我的代码:

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) 和 justification 几种方法。这是我能得到的最接近的):

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

我该怎么做?谢谢

【问题讨论】:

  • 您能否将您的预期输出格式化为列,以便我们知道您的预期?您的问题是两列,但您似乎有两列以上的数据。
  • 也许我不应该说专栏。第三部分代码包含我所期望的。

标签: c++ formatting output cout


【解决方案1】:

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

【讨论】:

  • 解决了!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
  • 2012-01-04
  • 1970-01-01
  • 2010-12-22
  • 2017-04-27
相关资源
最近更新 更多