向右显示输出

时间:2016-02-19 06:13:46

标签: c++

所以我对编程和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

2 个答案:

答案 0 :(得分:3)

我想这应该是直觉的。同时将setw添加到second print

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;
//                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^

答案 1 :(得分:0)

在您的cout语句中添加正确

这是另一个相关的堆栈溢出帖子: Right Justifying output stream in C++

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

其中x是一个整数,表示以下&#34;输出&#34;的宽度。