How Can I Align Columns Neatly in C++ Output?

  • Thread starter Thread starter FrostScYthe
  • Start date Start date
  • Tags Tags
    Columns
Click For Summary

Discussion Overview

The discussion revolves around formatting output in C++ to align columns neatly in console output. Participants explore methods to achieve consistent spacing between numbers in tabular data, focusing on both built-in functions and custom solutions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their current output format using `setprecision` and expresses a desire for tidier, straight columns.
  • Another participant notes that the spacing between numbers appears uneven and seeks clarification on this issue.
  • A suggestion is made to implement a custom padding function similar to C#'s `String.PadLeft` and `String.PadRight`, although the participant admits limited knowledge of C++ specifics.
  • Another participant recommends using the `setw` manipulator in C++ to control column width and mentions the use of `left` and `right` for justification.
  • There is a suggestion to use code tags for better formatting of the output example shared by the first participant.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, with no consensus on a single solution. The discussion remains open with various suggestions and methods proposed.

Contextual Notes

Some limitations include the lack of clarity on specific C++ functions compared to C# methods, and the need for further exploration of formatting options available in C++.

FrostScYthe
Messages
80
Reaction score
0
Hey after I run my program in C++ I get my results this way with this line of code:

count << 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?
 
Computer science news on Phys.org
Damn you can't notice it her, but anyway I mean the spaces between the numbers are uneven, if you get what I mean.
 
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:
Use [ code ] ... [ /code ] tags for text you want to appear verbatim:

Code:
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)