This is the code I have right now :

        cout << "==============================\n";
        cout << setprecision(2) << fixed;
        cout << "Shelby" << setw(24) << shelby << endl;

And this is the output:

    ==============================
    Shelby                   25.81

But what I'm trying to do is add a "$" sign before the amount while keeping the last number in the line in line with the line above "===="

but when I put this code in:

        cout << "==============================\n";
        cout << setprecision(2) << fixed;
        cout << "Shelby" << setw(24) << "$" << shelby << endl;

I get the output:

    ==============================
    Shelby                       $25.81

Can anyone help me fix this?
(Keep in mind that sometimes the number left of the decimal can reach triple digits or may only be 1 digit)

Recommended Answers

All 3 Replies

1st solution - Increase cout << "==============================\n"; by specific amount.
2nd solution - Decrease the setw() argument. For example setw(19).

What you need to do is adjust 24 so that the string "$" is positioned making allowance for (number of digits before the decimal point ) + 3 digits.
Why 3? Decimal Point + 2 Digits after the decimal place

So in this case, it should work if you wrote
cout << "Shelby" << setw(24-5) << "$" << shelby << endl;

Now to figure out the number of digits before the decimal point.

if( 0 < value < 10 )
    x = 1
if( 10 <= value < 100 )
    x = 2
cout << "Shelby" << setw( 24 - (x + 3) ) << "$" << shelby << endl;
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.