0

我的输出未与 $ 对齐

我在我所有的 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 但没有变化。这是一个小项目,我试图匹配正确的结果。请分享一些建议,我期待着阅读它。

4

1 回答 1

0

正如有人已经指出的那样,您可以使用空格来帮助实现您想要的格式

#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
于 2019-02-18T08:33:58.390 回答