如何正确调整各种长度的浮子?

时间:2019-02-10 01:50:53

标签: c++

对于我的代码的最后一列,我想对输出进行对齐。不幸的是,即使更改标志,我也无法这样做。我对setw的目的没有彻底的了解。

没有其他空格,缩进或代码可以解决此问题。

#include <iostream>
#include <iomanip>
using namespace std;

const float TVPRC = 400.00;

const float RECOPRC = 35.30;


int main(){
    int tv, rc;
    cout << "How many TVs were sold?     ";
    cin >> tv;

    cout << "\nHow many remote controllers were sold?     ";
    cin >> rc;


    float costtv;
    float costrc;
    cout<< setprecision(2);
    costtv = TVPRC * tv;
    costrc = RECOPRC * rc;

    float subttl, tax, ttl;
    subttl = costtv + costvcr + costcd + costtr + costrc;
    tax = subttl*TAX;
    ttl = subttl + tax;


    cout.setf(ios::fixed);
    cout << endl << setw(5) << "QTY";
    cout << setw(2) << "    " << "DESCRIPTION"; //<< setw(9) << "DESCRIPTION" << setw(9) << "Unit Price" << setw(9) << "TotalPrice";    
    cout << setw(2) << "    " << "UNIT PRICE";  
    cout << setw(2) << "    " << "TOTAL PRICE" << endl;
    cout.setf(ios::left);
    cout << setw(5) << "  ---   " << setw(5) << "-----------    "<< setw(5) << "----------  " << setw(5) << "------------";

    cout.unsetf(ios::left);
    cout.setf(ios::right);
    cout << endl << setw(4) << tv;
    cout.setf(ios::left);
    cout << setw(2) << "    " << "TV    "; 
    cout << setw(8) << "      " << TVPRC<< "    ";
    cout.setf(ios::left);
    cout << setw(2) << costtv << endl;


    cout.setf(ios::right);
    cout << endl << setw(4) << rc;
    cout.setf(ios::left);
    cout << setw(2) << "    " << "REMOTE CNTRLR "; 
    cout << setw(8)  << RECOPRC<< " ";
    cout.setf(ios::left);
    cout << setw(2) << costrc << endl;

    return 0;

}

0 个答案:

没有答案
相关问题