#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.

Dani AI

Generated

Short, practical clarification

The core issue is the difference between sticky format flags and one-shot manipulators. std::setw applies only to the very next insertion. Manipulators such as std::left, std::right, std::fixed and std::setprecision set stream flags that persist until changed. That explains why printing a literal "$ " separately then calling setw often produces misaligned output: the width was applied only to the numeric insertion that followed, not to the currency symbol.

A simple, reliable pattern is to build the formatted numeric text first, then output that whole string with setw and right. This keeps the dollar sign and the digits in one field so padding works predictably.

Example (format number into a string, then align the combined string):

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

double amount = 22.0;
std::ostringstream tmp;
tmp << std::fixed << std::setprecision(2) << amount;
std::string money = std::string("$ ") + tmp.str();

std::cout << std::setw(22) << std::left << "Registration Fee"
          << std::setw(8)  << std::right << money << '\n';

Notes and quick tips

  • As pointed out, moving the "$ " changes what setw targets; the ostringstream approach makes that explicit and easier to reason about.
  • ’s suggestion to use C-style printf is a valid alternative (printf("%-22s %8.2f\n", label, value);) when exact column control is needed.
  • setw counts characters after formatting (decimal point, sign, digits). If fields still misalign, print the intermediate string (in quotes) to inspect lengths.
  • If true decimal-point alignment (decimal columns lined up regardless of integer width) is required, split integer and fraction parts and pad the integer part; stream manipulators only provide string-end alignment, not automatic decimal-column alignment.

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.