Formatting with leading 0s and spaces in the same line

The code computes, but I need it to align like this:
QTY--Description--------Unit Price------Total Price
XX---TV------------------XXXX.XX-------XXXXX.XX

With spaces instead of the dashes (site won't let me format like that)

I have spent over 2 hours trying to do this with no avail. I believe I am supposed to use setfill('0') << setw()?
But then when I do that I can only do the 0s and setw no longer allows me to add spaces in between, even if i reset setfill to (' ').
Also the QTY needs to be aligned to the left, but only allows me to add an extra 0 if it is aligned to the right, otherwise the 0 goes after the number. (QTY is a single digit number)


(This is only a snip of the code)
1
2
3
    cout << setprecision(2) << fixed;
    cout << left << setw(12) << "QTY" << setw(20) << "Description" << setw(20) << "Unit Price" << setw(20) << "Total Price" << endl;
    cout << right << setw(2) << TV << left << setw(20) << "TV" << setw(20) << TVPrice << setw(20) << TotalPriceTV << endl;



If somebody could rewrite this code so it matches the format I would greatly appreciate it.
Last edited on
closed account (48T7M4Gy)
setfill()

http://www.cplusplus.com/reference/iomanip/setfill/?kw=setfill
I already tried that like I said in the OP but it won't let me use both setfill('0') and setfill(' ') in the same line. Still need help please
If the problem is that you're not printing the number of decimal places correctly then perhaps you need to add a call to showpoint() as well.

http://www.cplusplus.com/reference/ios/showpoint/

It isn't the decimal point. Maybe I should have elaborated:

my attempt using setfill:
1
2
3
4
5
cout << setprecision(2) << fixed;

cout << left << setw(12) << "\n\nQTY" << setw(20) << "Description" << setw(20) << "Unit Price" << setw(20) << "Total Price" << endl;

cout << right << setfill('0') << setw(2) << TV << left << setfill(' ') << setw(20) << "TV" << setfill('0') << setw(7) << TVPrice  << setw(8) << TotalPriceTV  << endl;


Result:
QTY----------Description--------------------Unit Price--------------------Total Price
05TV--------------------500.0002500.000

With dashes as spaces, exactly like that.

Problem 1: QTY needs to be right justified so that the 0 will appear at the beginning. Description needs to be left justified so the items below it will all line up. Now I am left with 05TV, no clue how to add spaces in between?

Problem 2: Since the following items are right justified, the 0 that i need to be leading is coming up as 500.000 and 2500.000 instead of 0500.00 and 02500.00

Problem 3: Need to add a spacing between 500.000 and 2500.000, I've tried using setfill twice here but it's not working

Hope somebody can help me. Thanks
Last edited on
Spaces are preserved within [output] tags.
QTY          Description                    Unit Price                   Total Price
05TV                    500.0002500.000


1: You want a space between the number 05 (in variable TV) and the description (the string "TV")? If so, why not just add a space in front of the string "TV"? (i.e., " TV".)

2: You mean "since they are left-justified". That's why the padding is on the right. If you right justify them, does it not work? Or do you want them right justified with spaces and a limited number of leading zeroes?

3: Then pad with spaces rather than zeroes, unless I'm misunderstanding you. You are getting no spaces because 500.00 is being padded on the right with zeroes (exactly 1, in fact, since it needs to pad from 6 to 7 characters).
closed account (48T7M4Gy)
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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	cout << "QTY--Description--------Unit Price------Total Price\n";
	//       XX-- - TV------------------XXXX.XX------ - XXXXX.XX
	int qty = 5;
	string description = "Widget";
	double price = 14.231;
	double totalPrice = qty * price;
	
	cout << setw(4);
	cout << setfill('*');
	cout << qty;

	cout << '\t';
	cout << setw(12);
	cout << setfill('^');
	cout << left;
	cout << description;

	cout << '\t';
	cout << setw(12);
	cout.precision(4);
	cout << setfill('-');
	cout << right;
	cout << '$' << price;
}


Take your pick. :)

http://www.cplusplus.com/reference/ios/fixed/ also might be useful along with precision.
Last edited on
Finally got it working. Used spaces as padding and something was blocking my leading zeros. Your code helped a lot, kemort. Thanks everyone
Topic archived. No new replies allowed.