aligning numbers in C++ (layout problem)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deviate
    New Member
    • Aug 2007
    • 11

    aligning numbers in C++ (layout problem)

    Hi guys :)

    ok so i have something like this

    Code:
    for(i=0;<10;i+)
    {
         cout<<"\n\n"<<setw(15)<<somenumber[i]<<setw(25)<<someinteger[i];
    }
    The problem is that because the numbers from somenumber[] are different lengths, the numbers from someinteger[] dont come out aligned nicely under eachother :( how do i fix this? Is there a command that could align them to the right? Thx guys! :)
  • kreagan
    New Member
    • Aug 2007
    • 153

    #2
    Originally posted by Deviate
    Hi guys :)

    ok so i have something like this

    Code:
    for(i=0;<10;i+)
    {
         cout<<"\n\n"<<setw(15)<<somenumber[i]<<setw(25)<<someinteger[i];
    }
    The problem is that because the numbers from somenumber[] are different lengths, the numbers from someinteger[] dont come out aligned nicely under eachother :( how do i fix this? Is there a command that could align them to the right? Thx guys! :)
    maybe you can insert a if statement that states (assuming that numbers ranges from 1 - 1000 for this example)

    if ( somenumber[i] < 100) { add 2 spaces }
    else if ( some number[i] < 1000) { add 1 spaces }

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      You shouldn't have to worry about the size of the int using setw. It's suppose to take care of that automatically.

      With setw you should have gotten numbers printed with some constant spacing. I believe the default justification is right-justified. Try left instead.
      Code:
      cout << left << setw( .....

      Comment

      Working...