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:";

 cin>>month;

 cout<<"Please Enter year:";

 cin>>year;

 cout<<"Please Enter Total:";

 cin>>total;

 sale=total/1.06;

 cstax=0.02*sale;

 sstax=0.04*sale;

 tstax=0.06*sale;

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

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

 cout<<"---------------------------------"<<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

Relevance
  • 4 weeks ago
    Best 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;

    ...etc.

    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.

  • Anonymous
    4 weeks 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;

    }

  • Chris
    Lv 7
    4 weeks ago

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

  • VP
    Lv 7
    4 weeks 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! 🤣

  • How do you think about the answers? You can sign in to vote the answer.
  • 4 weeks 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.

Still have questions? Get your answers by asking now.