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

  • Thread starter Thread starter FrostScYthe
  • Start date Start date
  • Tags Tags
    Columns
Click For Summary
To format output in C++ for improved readability, the use of the `setw` manipulator is recommended. This allows for setting a specific width for each output field, ensuring that columns align properly. Additionally, the `left` and `right` manipulators can be employed to control text justification within those widths. The existing output method using `cout << setprecision(7)` provides numerical precision but does not address column alignment, which can lead to uneven spacing in the output. For those familiar with C#, similar functionality can be achieved through custom padding functions, but C++ offers built-in manipulators that simplify this process.
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:

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?
 
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)
 
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...