Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help straightening columns

  1. Aug 25, 2006 #1
    Hey after I run my program in C++ I get my results this way with this line of code:

    cout << setprecision(7) << fa << '\t' << fm << '\t' << fabs(b-a);

    f(a) f(m) error
    -308 154.375 4.5
    -308 25.70313 2.25
    -308 -111.248 1.125
    -111.248 -35.8313 0.5625
    -35.8313 -3.395538 0.28125
    -3.395538 11.56259 0.140625
    -3.395538 4.186766 0.0703125
    -3.395538 0.4215546 0.03515625

    I want to make it tidier, and for the columns to be straight, anyone know how to do that easily?
     
  2. jcsd
  3. Aug 25, 2006 #2
    Damn you can't notice it her, but anyway I mean the spaces between the numbers are uneven, if you get what I mean.
     
  4. Aug 26, 2006 #3

    -Job-

    User Avatar
    Science Advisor

    C# has the methods String.PadLeft(size, char), String.PadRight(size, char) which adds a number of characters to the left, or right, of the string so that the final length will be the specified size.
    Doing your own padding function is simple. In pseudo code:

    function PadRight(string str, int size, char c){
    int diff = size - str.Length;
    for(int i=0; i<diff; i++){
    str.append(c);
    }
    return str;
    }

    Then you Pad your numbers before outputting them.
    In C, with printf, you can specify flags that do this automatically, i'm not sure in C++, i'm not very rotated in that language.
    This should probably be better off int he programming forum.
     
    Last edited: Aug 26, 2006
  5. Aug 26, 2006 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Use [ code ] ... [ /code ] tags for text you want to appear verbatim:

    Code (Text):

    f(a)    f(m)    error
    -308    154.375 4.5
    -308    25.70313        2.25
    -308    -111.248        1.125
    -111.248        -35.8313        0.5625
    -35.8313        -3.395538       0.28125
    -3.395538       11.56259        0.140625
    -3.395538       4.186766        0.0703125
    -3.395538       0.4215546       0.03515625
     
    Anyways, why not use the setw manipulator? (And use left and right if you want to control the justification)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Need help straightening columns
  1. Need help (Replies: 5)

  2. Need help! (Replies: 10)

  3. New to GPS and need help (Replies: 19)

Loading...