Thread: formatting cout

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    formatting cout

    I have a point, let's say a 3D.
    Of course the coordinates can be any value in Real.
    I want them to be printed nicely.

    I used (from std) filled, setw, width, left, right, internal and more but I can not get what I want. I can do it with printf, but I want to learn the C++ way.

    For example, if I have:
    Code:
    -1    2     1.68787
    1.132536 4 21.3
    I want to be printed like this:
    Code:
    -1           2     1.68787
     1.132536    4     21.3
    In other words, I want the coordinates to be alligned.
    Last edited by std10093; 09-15-2013 at 06:19 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Can you show the printf statement that generates that output?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I should have done it from the first post! Sorry.
    What actually troubles me with printf, except the fact that this is C, is that I have to know the exact max number of the digits of the coordinates. However, setting a usually big number does not seem a problem, is it?

    Code:
    printf("%5f %5f %5f\n", vertices[i].x(), vertices[i].y(), vertices[i].z()); // same for 2 instead of 5
    Code:
    0.000000 1.000000 1.618034
    0.000000 1.000000 -1.618034
    0.000000 -1.000000 -1.618034
    0.000000 -1.000000 1.618034
    1.000000 1.618034 0.000000
    1.000000 -1.618034 0.000000
    -1.000000 1.618034 0.000000
    -1.000000 -1.618034 0.000000
    1.618034 0.000000 1.000000
    1.618034 0.000000 -1.000000
    -1.618034 0.000000 1.000000
    -1.618034 0.000000 -1.000000
    Code:
    printf("%10f %10f %10f\n", vertices[i].x(), vertices[i].y(), vertices[i].z());
    Code:
    Vertices = 12
      0.000000   1.000000   1.618034
      0.000000   1.000000  -1.618034
      0.000000  -1.000000  -1.618034
      0.000000  -1.000000   1.618034
      1.000000   1.618034   0.000000
      1.000000  -1.618034   0.000000
     -1.000000   1.618034   0.000000
     -1.000000  -1.618034   0.000000
      1.618034   0.000000   1.000000
      1.618034   0.000000  -1.000000
     -1.618034   0.000000   1.000000
     -1.618034   0.000000  -1.000000
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try this:
    Code:
    printf("% 6.2f  % 6.2f  % 6.2f\n", vertices[i].x(), vertices[i].y(), vertices[i].z());
    Note the space before the 6 (and the limit to 2 decimal points).
    The space causes a space to be printed in from of a positive number.

    I know it's not C++ yet, but ....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I do not want the decimal points to be hided. The printf already gave me good results. With a little bit of search, I can get good˛ results.
    I am interested in C++ however. Thanks for the suggestion by the way!

    Code:
    printf("% 10f  % 10f  % 10f\n", vertices[i].x(), vertices[i].y(), vertices[i].z());
    Something like this would be nice in C.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I was curious about your initial post in which some of the numbers didn't show any decimal places (trailing zeroes). I was wondering about the printf format that achieved that. But I gather that you don't mind having the trailing zeroes, which is good.

    So what about:
    Code:
        std::cout << std::setw(-10) << vertices[i].x() << "  "
                  << std::setw(-10) << vertices[i].y() << "  "
                  << std::setw(-10) << vertices[i].z() << '\n';
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Your code gave me:
    Code:
    0  1  1.61803
    0  1  -1.61803
    0  -1  -1.61803
    0  -1  1.61803
    1  1.61803  0
    1  -1.61803  0
    -1  1.61803  0
    -1  -1.61803  0
    1.61803  0  1
    1.61803  0  -1
    -1.61803  0  1
    -1.61803  0  -1
    which is not any special formatting.. :/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What do you expect for free?
    I must have ran the C code instead because it looked great!
    But now that I try it again, you're right. It sucks.
    Sorry about that.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    No problem. If no C++ solution comes up, I am going to go for the printf version.

    Maybe this can do the trick.
    Code:
    std::cout<<std::setfill (' ')<<std::left<<std::setw(10)<<vertices[i].x()<< " " <<
                     std::setfill (' ')<<std::left<<std::setw(10)<<vertices[i].y()<<  " " << 
                     std::setfill (' ')<<std::left<<std::setw(10)<<vertices[i].z()<< "\n";
    Last edited by std10093; 09-15-2013 at 07:52 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Or this
    Code:
        std::cout.setf(std::ios::fixed, std::ios::floatfield);
        std::cout << std::setw(10) << vertices[i].x() << "  "
                  << std::setw(10) << vertices[i].y() << "  "
                  << std::setw(10) << vertices[i].z() << '\n';
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also please note that the std::setfill() and std::left are sticky. Which means that they stay in the new state until they are changed. This is unlike setw() which must be used for each output.

    The following would be all you would need.
    Code:
    std::cout << std::setfill(' ') << std::left 
                  << std::setw(10) << vertices[i].x() << " "
                  << std::setw(10) << vertices[i].y() <<  " "
                  << std::setw(10) << vertices[i].z() << "\n";
    If you're going to use the stream after this you should be sure to reset the stream back to "normal". The other option would be to use a stringstream to format a string for printing instead of playing with the stream.

    Also you really should start to think about using a little extra whitespace in your code, it'll make reading much easier.

    Jim

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The only problem with that is that it looks terrible.
    This does what he originally wanted:
    Code:
    #include <iostream>
    #include <iomanip>
    
    void out(double x, double y, double z)
    {
        std::cout << std::setw(12) << x << ' '
                  << std::setw(12) << y << ' '
                  << std::setw(12) << z << '\n';
    }
    
    int main()
    {
        std::cout << std::setfill(' ') << std::right << std::fixed;
        out(-1,       -1.61803,  0);
        out( 1.61803,  0,        1);
        out(-1,       -1.618034, 0);
        return 0;
    }
    
    /* Output:
    
       -1.000000    -1.618030     0.000000
        1.618030     0.000000     1.000000
       -1.000000    -1.618034     0.000000
    */
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Thank you both. oogabooga's solution is very good.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Actually since the default setfil() is " " you don't even need the setfill(), the same with the right, it too is the default so it isn't actually needed. The std::fixed and perhaps a setprecision(), if you want less than the default number of digits, would be all you would need.

    Code:
    #include <iostream>
    #include <iomanip>
    
    void out(double x, double y, double z)
    {
        std::cout << std::setw(12) << x 
                  << std::setw(12) << y 
                  << std::setw(12) << z << '\n';
    }
    
    int main()
    {
        std::cout <<  std::fixed << std::setprecision(4);
    
        out(-1,       -1.61803,  0);
        out( 1.61803,  0,        1);
        out(-1,       -1.618034, 0);
    
        return 0;
    }
    You also don't need to print that extra space, just use the proper setw() size to allow the proper space between the fields.

    Also in my post #11 I was only pointing out the fact that several of the calls were unnecessary, I didn't actually try the code and wasn't commenting on what output the code would produce.

    Jim

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are right. That's ok.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cout formatting
    By disruptivetech in forum C++ Programming
    Replies: 10
    Last Post: 01-09-2008, 04:29 PM
  2. float formatting using cout
    By JJFMJR in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2007, 10:31 PM
  3. Simple cout formatting question.
    By guitarist809 in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2006, 12:59 PM
  4. Whats the difference between cout and std::cout?
    By mdshort in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 05:34 PM
  5. formatting output with cout
    By boojus in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 07:17 AM