#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

        cout << "\n0----------------------1------8\n";
        cout << setw(22) << left << "Registration Fee     ";
        cout << setw(8) << right << fixed << setprecision(2) << "$ "  << 22.0 << endl;
        cout << setw(22) << left << "Hotel Total          ";
        cout << setw(8) << right << fixed << setprecision(2) << "$ " << 350.00 << endl;
        cout << setw(22) << left << "Transportation Total ";
        cout << setw(8) << right << fixed << setprecision(2) << "$ "  << 5.00 << endl;
        cout << setw(22) << left << "Meals Total          ";
        cout << setw(8) << right << fixed << setprecision(2) << "$ "  << 5000.00 << right;
        cout << "\n--------------------------------------------\n";






    return 0;
}

All I want is two columns, with the stuff in it left aligned, and a second with decimals right aligned. Everytime I think I understand this #$^%$#ing formatting shi...er stuff, I come to realize I'm utterly clueless. Sometimes it will work fine, and I think great! I get it, then like the facacta example above, the output will, instead of giving me numbers lined up at the decimal, it gives me the finger. Can someone please break it down for me. All the examples I can find are of the most basic kind.

From what I understand set() creates a column that will hold something less than it's size, and << right or << left will justifiy it.

Thanks for any help on this incredibly rookie problem

(please note, the example above is just one of many variations I've tried)
Thanks again.

Recommended Answers

All 6 Replies

From what I understand set() creates a column that will hold something less than it's size, and << right or << left will justifiy it.

Correct. But note that with the modifiers, typically they only affect the next item in the chain, not all further items.

Thanks, but still not getting it. The first chunk below lines up the decimals, but my "$"'s are too far, the second one fixes that and then the decimals are all F'd. I feel like I'm just spinning my wheels dicking around with the placement of the modifiers and still seeing no pattern.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

        cout << "\n0----------------------1------8\n";
        cout << setw(22) << left << "Registration Fee     ";
        cout  << fixed << setprecision(2) <<setw(8)  << right << "$ " << 22.0 << endl;
        cout << setw(22) << left << "Hotel Total          ";
        cout <<  fixed << setprecision(2) << setw(8)  << right << "$ " << 350.00 << endl;
        cout << setw(22) << left << "Transportation Total ";
        cout  << fixed << setprecision(2) <<setw(8)  << right << "$ " << 5.00 << endl;
        cout << setw(22) << left << "Meals Total          ";
        cout <<  fixed << setprecision(2)<<setw(8)  << right << "$ "  << 5000.00 << endl;
        cout << "\n--------------------------------------------\n";




        cout << "\n0----------------------1------8\n";
        cout << setw(22) << left << "Registration Fee     ";
        cout  << fixed << setprecision(2) << "$ " << setw(8) << right << 22.0 << endl;
        cout << setw(22) << left << "Hotel Total          ";
        cout <<  fixed << setprecision(2) << "$ " << setw(8) << right<< 350.00 << endl;
        cout << setw(22) << left << "Transportation Total ";
        cout  << fixed << setprecision(2) << "$ "<< setw(8)  << right  << 5.00 << endl;
        cout << setw(22) << left << "Meals Total          ";
        cout <<  fixed << setprecision(2) << "$ " <<setw(8)  << right << 5000.00 << endl;
        cout << "\n--------------------------------------------\n";







    return 0;
}

Well, you could always fall back on the old C standard fprintf() format strings. Sometimes it is a lot easier than C++ output manipulators, though if your class is trying to teach you C++ output manipulators then that may not be an option... :-)

Yeah, I've used prinf in C# and C. This isn't for a class, i'm just going through an old text book and doing every problem at the end of each chapter, because I want to be constantly practicig, and there's things like the afore mentioned formatting deals, in addition to things like fstream and switch, which are all simple, but I've only used a little, I think it best to become proficient in the easy stuff, which makes the tougher stuff easier to digest when you don't have to worry about the simpler things. I'll keep plugging away at it, and will figure it out on the eventual. Thanks though

The first chunk below lines up the decimals, but my "$"'s are too far, the second one fixes that and then the decimals are all F'd.

Right. The modifiers apply to the next item. If you don't want the "$" to be the next item, move it to before the modifiers:

cout << "$ " << fixed << setprecision(2) << setw(8) << right << 22.0 << endl;

OH! GOT IT! I was treating the << right and << left like the setprecision() and fixed which apply to everything in the output. So to fix it I just needed to set a column using setw() of appropriate size for each item I wanted lined up and then place the appropriate (<< right or << left) modifier right before each new "column". Well sonofa. Thanks man.Using these stupid things correctly has been nagging at me for awhile. Like I said, painfully simple, but makes sense now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.