[英]My output is suppose to align on the $ like my professors. However I'm not sure how to use setw()
我的输出与$不一致
我在所有cout上尝试了cout << left << setw(1)
#include <iostream>
#include <ios>
#include <iomanip>
using namespace std;
int main() {
// insert code here...
float RENT_OR_MORTGAGE;
float UTILITIES;
float PHONE;
float CABLE;
// H stands for housing
float TOTAL_MONTHLY_HOUSING_COSTS;
float TOTAL_ANNUAL_HOUSEING_COSTS;
char dollars = '$';
cout << "Enter your monthly costs for the following: " << endl << endl;
cout << "Rent or Mortgage: ";
cin >> dollars >> RENT_OR_MORTGAGE;
cout << "Utilities: " << setw(10) << left;
cin >> dollars >> UTILITIES;
cout << "Phone(s): " << setw(10) << left;
cin >> dollars >> PHONE;
cout << "Cable: " << setw(10) << left;
cin >> dollars >> CABLE;
TOTAL_MONTHLY_HOUSING_COSTS = RENT_OR_MORTGAGE + UTILITIES + PHONE + CABLE;
TOTAL_ANNUAL_HOUSEING_COSTS = (RENT_OR_MORTGAGE + UTILITIES + PHONE + CABLE) * 12;
cout << fixed << setprecision(2) << setw(dollars) << left << "Total monthly housing costs: " << dollars << TOTAL_MONTHLY_HOUSING_COSTS << endl << setw(dollars) << left << "Total annual housing costs: " << dollars << TOTAL_ANNUAL_HOUSEING_COSTS << endl;
return 0;
I want an output of
Enter your monthly costs for the following:
Rent or mortgage: $1348
Utilities: $215
Phone(s): $99
Cable: $69
Total monthly housing costs: $ 1731.00
Total annual housing costs: $20772.00
my output is
Enter your monthly costs for the following:
Rent or Mortgage: $1348
Utilities: $512
Phone(s): $99
Cable: $69
Total monthly housing costs: $2028.00
Total annual housing costs: $24336.00
我希望它与$对齐。 我尝试了setw(),但我会不断更改内部的数字,但没有任何变化。 我有#include但没有任何变化。 这是一个小项目,我正在尝试匹配正确的结果。 请分享一些建议,我期待阅读。
正如有人已经指出的那样,您可以使用空格来帮助实现所需的格式
#include <iostream>
#include <ios>
#include <iomanip>
using namespace std;
int main() {
float RENT_OR_MORTGAGE;
float UTILITIES;
float PHONE;
float CABLE;
// H stands for housing
float TOTAL_MONTHLY_HOUSING_COSTS;
float TOTAL_ANNUAL_HOUSEING_COSTS;
cout << "Enter your monthly costs for the following: " << endl << endl;
cout << "Rent or Mortgage: $";
cin >> RENT_OR_MORTGAGE;
cout << "Utilities: $";
cin >> UTILITIES;
cout << "Phone(s): $";
cin >> PHONE;
cout << "Cable: $";
cin >> CABLE;
TOTAL_MONTHLY_HOUSING_COSTS = RENT_OR_MORTGAGE + UTILITIES + PHONE + CABLE;
TOTAL_ANNUAL_HOUSEING_COSTS = (RENT_OR_MORTGAGE + UTILITIES + PHONE + CABLE) * 12;
int digits = (TOTAL_ANNUAL_HOUSEING_COSTS * 100 / 100);
cout << "Total monthly housing costs: $" << setw(to_string(digits).length() + 3) << fixed << right << setprecision(2) << TOTAL_MONTHLY_HOUSING_COSTS << endl;
cout << "Total annual housing costs: $" << setw(to_string(digits).length() + 3) << fixed << right << setprecision(2) << TOTAL_ANNUAL_HOUSEING_COSTS << endl;
return 0;
}
产量
Enter your monthly costs for the following:
Rent or Mortgage: $1348
Utilities: $215
Phone(s): $99
Cable: $69
Total monthly housing costs: $ 1731.00
Total annual housing costs: $20772.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.