setw() alignment help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

setw() alignment help

Im doing a program where I have to align everything, and I was told to put something like for ex: setw(9)<<right<<etc..so it could align.
when I test out my program, the output comes out with number that are not aligned. I also heard you could do something about adding to the setw but it had to do with something I can remember. Here is the setw() thing I am doing:

cout << "Account infrormation"<<setw(20) << endl;
cout << "Account owner:"<<setw(20) << fullname << endl;
cout << "Account number:"<<setw(20) << accountnumber << endl;
cout << "Account type:"<<setw(20)<<"Checking" << endl;
cout << "Minimum balance"<<setw(20)<< minimumbalance << endl;
cout << "Old balance:"<<setw(20) << oldbalance << endl;
cout << "New balance:"<<setw(20)<< newbalance << endl;

I want the number to align correctly, and not to un-align when bigger number appear. What can I do?

The output looks like this:

----Account infrormation
------Account owner:--------Frank Rivers
Account number:---------------46728
Account type:-------------Savings
Minimum balance:----------------1000
Old balance:----------------2700
New balance:-----------------108

Again, What can I do to align to the right?
NOTE: I am not doing a setfill, i added the dashes because they do not space the spaces out when I post. That is how the output looks like, just take out the dashes.

Comments

  • Ed HallEd Hall Posts: 395Member
    [code]
    : Im doing a program where I have to align everything, and I was told
    : to put something like for ex: setw(9)<<right<<etc..so it could
    : align.
    : when I test out my program, the output comes out with number that
    : are not aligned. I also heard you could do something about adding to
    : the setw but it had to do with something I can remember. Here is the
    : setw() thing I am doing:
    :
    : cout << "Account infrormation"<<setw(20) << endl;
    : cout << "Account owner:"<<setw(20) << fullname << endl;
    : cout << "Account number:"<<setw(20) << accountnumber << endl;
    : cout << "Account type:"<<setw(20)<<"Checking" << endl;
    : cout << "Minimum balance"<<setw(20)<< minimumbalance << endl;
    : cout << "Old balance:"<<setw(20) << oldbalance << endl;
    : cout << "New balance:"<<setw(20)<< newbalance << endl;
    :
    : I want the number to align correctly, and not to un-align when
    : bigger number appear. What can I do?
    :
    : The output looks like this:
    :
    : ----Account infrormation
    : ------Account owner:--------Frank Rivers
    : Account number:---------------46728
    : Account type:-------------Savings
    : Minimum balance:----------------1000
    : Old balance:----------------2700
    : New balance:-----------------108
    :
    : Again, What can I do to align to the right?
    : NOTE: I am not doing a setfill, i added the dashes because they do
    : not space the spaces out when I post. That is how the output looks
    : like, just take out the dashes.
    [/code]
    First, you can use code tags to allow for proper display of your text.

    I think you are currently telling the second output to be 20 spaces wide "after" the end of your first output. You need to take into account the width of the first output and subtract it from the overall.

    Try something like:

    [code]
    cout << "Account owner:"<<setw(20) << fullname << endl;
    cout << "Account number:"<<setw(19) << accountnumber << endl;
    cout << "Account type:"<<setw(21)<<"Checking" << endl;
    cout << "Minimum balance:"<<setw(18)<< minimumbalance << endl;
    [/code]

    Take Care,
    Ed
  • TheBizzTheBizz Posts: 1Member
    this thread is pretty old, but since it's the first search result in google, I think this reply would be useful somehow...

    [code]cout<<left<<setw(18)<<"Account owner:"<<right<<setw(8)<<fullname<<endl;
    cout<<left<<setw(18)<<"Account number:"<<right<<setw(8)<<accountnumber<<endl;
    cout<<left<<setw(18)<<"Account type:"<<right<<setw(8)<<"Checking"<<endl;[/code]
    which prints: (dashes and pluses was originally spaces)
    [code]Account owner:----++++John
    Account number:---+++++++2
    Account type:-----Checking
    [/code]
    you can also use this:
    [code]cout<<left<<setw(18)<<"Account owner:"<<setw(8)<<fullname<<endl;[/code]
    which is equal to:
    [code]cout<<left<<setw(18)<<"Account owner:"<<left<<setw(8)<<fullname<<endl;[/code]
    and prints:
    [code]Account owner:----John++++[/code]
Sign In or Register to comment.