在右侧显示输出

Displaying output to the right

本文关键字:输出 显示      更新时间:2023-10-16

所以我对编程和C++很陌生。这个简单的程序是我的第二个程序,我需要一点帮助。下面是我的代码,后跟我得到的输出以及我希望它的样子。如果有人能指出我正确的方向,或者让我知道如何改变这一点,我们将不胜感激。

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
double propValue,    //Property Value
    assessment,   //Assessment
    srAssessment, //Sr Assessment
    taxRate,         //Tax rate
    annualPropTax,   //Annual Property tax
    quarterlyTax;  //Quarterly Tax
string name;
const double EXEMPT = 5000,      //shows the total after exemption
    QUARTER = 4,         //represents the amount of quarters in a year
    TAXPERHUNDRED = 0.01,  //represents tax rate for every $100
    SIXTYPERCENT = 0.6;  //Represents the tax based on 60% of original value

//Gets name from user
cout << "Please enter your full name: ";
getline(cin, name);
//gets property value from user
cout << "Enter the actual value of the property: ";
cin >> propValue;

//Gets tax rate
cout << "Enter the tax rate for each $100 of assessed value: ";
cin >> taxRate;
cout << endl << endl;
//Calculates assessment
assessment = propValue * SIXTYPERCENT;
//Calculates Sr. Assessment
srAssessment = assessment - EXEMPT;
//Calculates annual property tax
annualPropTax = srAssessment * taxRate * TAXPERHUNDRED;
//Calculates Quarterly tax
quarterlyTax = annualPropTax / QUARTER;

//Displays owners name
cout << "Property owner's name: " << name << endl;
cout << endl;
cout << setprecision(2) << fixed;
//Displays Assesment
cout << "Assessment: " << setw(18) << "$ " << srAssessment << endl;
//Displays Annual Property tax
cout << "Annual Property Tax" << setw(11) << "$ " << std::right << annualPropTax << endl;
//Displays Quarterly Property tax
cout << "Quarterly Property Tax" << setw(8) << "$ " << std::left << quarterlyTax;
cout << endl << endl;
}

这是当前输出:

Assessment:                  $ 175000.00
Annual Property Tax          $ 7177.50
Quarterly Property Tax       $ 1780.63

我需要它做的是显示如下:

Assessment:                  $ 175000.00
Annual Property Tax          $   7177.50
Quarterly Property Tax       $   1780.63

我想它应该是主动的。还将setw添加到第二次打印中:

cout << "Assessment: " << setw(18) << "$ " << setw(10) << std::right << srAssessment << endl;
//                                         ^^^^^^^^^^^^^^^^^^^^^^^^^
cout << "Annual Property Tax" << setw(11) << "$ " << setw(10) << std::right << annualPropTax << endl;
//                                                ^^^^^^^^^^^
cout << "Quarterly Property Tax" << setw(8) << "$ " << setw(10) << std::right << quarterlyTax;
//                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^

cout 语句中添加

这是另一个相关的堆栈溢出帖子:正确对齐C++输出流

std::cout << std::right << std::setw(x) << "output";

其中 x 是一个整数,表示以下"输出"的宽度。