Why am I not getting output right aligned in C++?

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

int main()


 float total,cstax,sstax,tstax,sale;

 int year;

 string month;

 cout<<"Please Enter Month:";


 cout<<"Please Enter year:";


 cout<<"Please Enter Total:";







 cout<<"\nMonth: "<<month<<" "<<year<<endl;


 cout << "Total Collected: $ " <<total<<endl;

 cout << "Sales: $ " <<sale<<endl;

 cout << "County Sales Tax: $ " <<cstax<<endl;

 cout << "State Sales Tax: $ " <<sstax<<endl;

 cout << "Total Sales Tax: $ " <<tstax<<endl;

 return 0;


I have written expected output in below. But output I am getting does not right align this. Thanks in advance!

Attachment image

5 Answers

  • 8 months ago
    Favorite Answer

    The field width must be set before every output field that is to be aligned.  I count 8 total spaces  total spaces in your format (up to 7 digits plus a decimal point) for the money fields, so you need "<< setw(8)" immediately before each output field.

        cout << "Total Collected:     $ " << setw(8) <<  total << endl;

        cout << "Sales:                    $ " << setw(8) <<  sale << endl;

        cout << "County Sales Tax: $ " << setw(8) <<  cstax << endl;


    The field width is the only formatting attribute that needs to be set every time.  Other attributes (like fixed, scientific, setprecision, ...) are "sticky" and remain set until you change them.  The width is reset to zero after each value (including strings) written to the stream.

    PS: Don't try to copy/paste that.  The spaces won't line up.  There's no fixed width "code" font available here, so I just typed enough spaces to approximately line up the dollar signs.

    • Commenter avatarLogin to reply the answers
  • Anonymous
    8 months ago

     cout << "Sales: $ "

    will print $ one space after Sales:

    formatting etc won't affect this. So you'll have to remove the $ character and print it by itself.

    Alignment left or right has to be set for each item too. Since you want to keep switching from left to right alignment. Also it doesn't remember alignments anyway.

    It is easiest with a function:-

    void printRow(const char*item, float value){

      cout << setw(20) << left << item 

     << " $" 

    <<  setw(10) << right << value << endl;


    • Commenter avatarLogin to reply the answers
  • Chris
    Lv 7
    8 months ago

    Here's my take, which uses a function: https://ideone.com/LhY2KY

    • Commenter avatarLogin to reply the answers
  • VP
    Lv 7
    8 months ago

    My changes: 

    cout << setprecision(2) << fixed << right << endl;

    cout << " Total Collected:     $ " << setw(8) << total << endl;

    cout << " Sales:                    $ " << setw(8) << sale << endl; 

    cout << " County Sales Tax: $ " << setw(8) << cstax << endl; 

    cout << " State Sales Tax:    $ " << setw(8) << sstax << endl; 

    cout << " Total Sales Tax:     $ " << setw(8) << tstax << endl; 

    -- edit --

    Sorry, gang -- I was off using CodeBlocks and didn't know you two had already answered his question! 🤣

    • Commenter avatarLogin to reply the answers
  • How do you think about the answers? You can sign in to vote the answer.
  • 8 months ago

    The alignment setting only works for the number parts.

    Each text string needs to be the same length; you have to add spaces between the end of the text: and the $ sign so they line up and give a fixed boundary for the numeric part to be displayed from.

    If the position of the dollar signs is still not lining up after that, the problem is then most likely a variable-width (proportionally spaced) font.

    For that, you need to switch to a fixed space font such as Courier, or use a more complex output formatting such as tables or tabs.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.