How to align without using iomanip

I have a homework problem we are supposed to output to a file and the data values are supposed to be right aligned which I thought it was supposed to be by default. I don't know what to do here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  void printCharges (int perdisc, float LAB,
                  float price,float labor,float carpet,float install,
                  float discount,float subtotal,float TAX, float total)
{
    outfile.setf(ios::fixed,ios::floatfield);
    outfile.precision(2);

    outfile << "\t\t\t\tCHARGES\n\n";
    outfile << "DESCRIPTION\t\t\t" << "COST/SQ.FT.\t\t"
            << "CHARGE/ROOM" << endl;
    outfile << "Carpet";
    outfile.width(20);
    outfile << price << "\t\t\t$" << carpet << endl;
    outfile << "Labor";
    outfile.width(20);
    outfile << LAB;
    outfile.width(13);
    outfile << "$" << labor << endl << endl;
    outfile << "INSTALLED PRICE";
    outfile.width(23);
    outfile << "$" << install << endl;
    outfile << "Discount";
    outfile.width(15);
    outfile << perdisc << "%";
    outfile.width(10);
    outfile << discount << endl << endl;
    outfile << "SUBTOTAL";
    outfile.width(25);
    outfile << "$" << subtotal << endl;
    outfile << "Tax";
    outfile.width(30);
    outfile << (TAX * subtotal) << endl;
    outfile << "TOTAL";
    outfile.width(25);
    outfile << "$" << total;

}
If you do calculate how many digits a value to print will have, then you can calculate how many whitespace characters must be printed first in order to produce the desired total column width.

Edit: Or not. Your actual problem is the $.
1
2
3
4
outfile.width(N);
[code]outfile << "$"; // print the '$' with N-1 whitespace before it

outfile  << total; // no width is set for this 

In ideal case
1
2
outf << 42;
$42.00

In other words: the $ is part of the number format and then the ostream::width work nicely. Such output formatting has never been necessary to me, so I cannot point to proper direction. You probably have to do the math, if <iostream> is the only allowed part of the standard library.
Last edited on
Topic archived. No new replies allowed.