这是我的代码:
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/