Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-18-2006, 11:57 AM
|
#1
|
Member
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547
Rep:
|
Aligning output in C++
C++ code snippet:
Code:
oout << "> Field errors: " << endl;
for (uint32_t j = 0; j < data_manager_->dimensions; j++) {
oout << " [" << j << "]: " << sims[best_clustering].field_errors[j] << '('
<< sims[best_clustering].field_perc_errors[j] << "%)\t"
<< "actual: " << full_field_sums[j] << endl;
}
Corresponding output from file:
Code:
> Field errors:
[0]: 5415.33(7.733%) actual: 70028.8
[1]: 2269.28(24.5848%) actual: 9230.4
[2]: 1767.62(9.75045%) actual: 18128.6
[3]: 0(0%) actual: 0
[4]: -0.121228(-46.6105%) actual: 0.260088
[5]: 190.567(19.1915%) actual: 992.98
[6]: -0.161786(-43.4808%) actual: 0.372086
[7]: -3548.85(-8.07076%) actual: 43971.7
[8]: 0.0166313(32.5586%) actual: 0.0510811
[9]: -702.616(-14.3506%) actual: 4896.08
[10]: 308.157(31.1562%) actual: 989.069
[11]: 297.283(28.3117%) actual: 1050.04
[12]: 13.7334(8.82541%) actual: 155.612
[13]: 0(0%) actual: 0
[14]: 0.000399999(100%) actual: 0.000399999
[15]: 0.108866(12.3723%) actual: 0.87992
[16]: -6.53767(-9.98065%) actual: 65.5034
My question is, how can I make sure that my text output is always aligned? This isn't as simple as detected if the number to be outputed is zero, because there may be other numbers printed with only a few characters that don't align. I tried using tabs as you can see from the code and it works most of the time, but not always.  Any tips? Thanks in advance! 
|
|
|
01-18-2006, 12:32 PM
|
#2
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
You're probably looking for the width manipulator. Try
Code:
cout.width(12);
cout << sims[best_clustering].field_errors[j]
There is also floatfield, precision showpos, and showpoint that you may find useful.
graeme.
|
|
|
01-18-2006, 01:21 PM
|
#3
|
Member
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547
Original Poster
Rep:
|
Thanks, I appreciate it. I will look into those functions. 
|
|
|
01-18-2006, 05:30 PM
|
#4
|
Member
Registered: Jan 2006
Location: Lexington, KY
Distribution: Arch and a little Slack
Posts: 139
Rep:
|
Quote:
Originally Posted by graemef
You're probably looking for the width manipulator. Try
Code:
cout.width(12);
cout << sims[best_clustering].field_errors[j]
There is also floatfield, precision showpos, and showpoint that you may find useful.
graeme.
|
I was wondering that myself ... right now I just find the length of the string using strlen() and write a for loop to pad the colum widths. 
|
|
|
01-18-2006, 05:37 PM
|
#5
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
The following site may be of help if you are having any problems....
cout Man page
graeme
|
|
|
02-02-2006, 02:45 PM
|
#6
|
Member
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547
Original Poster
Rep:
|
I still can't seem to get these precision flags to do what I want them to, and it's really starting to annoy the hell out of me.  Here is my code and the output right now:
Code:
oout.width(20);
oout << showpos;
oout << showpoint;
oout << setfill(' ');
oout << setprecision(20);
for (uint32_t j = 0; j < data_manager_->dimensions; j++) {
oout << " [" << j << "]: " << run_results_[best_clustering_]->field_errors_[j]
<< " (" << run_results_[best_clustering_]->field_perc_errors_[j] << "%)\t"
<< "actual: " << full_field_sums_[j] << '\n';
}
Code:
> Field errors:
[0]: +0.016414642333984375000 (+0.029497763141989707947%) actual: +55.647075653076171875
[1]: +0.0014805793762207031250 (+0.020263686776161193848%) actual: +7.3065648078918457031
[2]: +0.0010566711425781250000 (+0.0098812934011220932007%) actual: +10.693652153015136719
[3]: +0.0000000000000000000 (+0.0000000000000000000%) actual: +0.0000000000000000000
[4]: +0.00023199548013508319855 (+5.6105451583862304688%) actual: +0.0041349898092448711395
[5]: +6.4849853515625000000e-05 (+0.0025120282080024480820%) actual: +2.5815734863281250000
[6]: +0.00095099583268165588379 (+10.081602096557617188%) actual: +0.0094329835847020149231
[7]: -0.0043716430664062500000 (-0.016516048461198806763%) actual: +26.469060897827148438
[8]: +1.1999742127954959869e-05 (+1.0516906976699829102%) actual: +0.0011409954167902469635
[9]: +0.0024447441101074218750 (+0.061942495405673980713%) actual: +3.9467961788177490234
[10]: +9.8973512649536132812e-05 (+0.063118480145931243896%) actual: +0.15680591762065887451
[11]: -0.0044142007827758789062 (-0.85196357965469360352%) actual: +0.51812082529067993164
[12]: +0.00081296265125274658203 (+0.62002408504486083984%) actual: +0.13111791014671325684
[13]: +0.0000000000000000000 (+0.0000000000000000000%) actual: +0.0000000000000000000
[14]: +2.4000066332519054413e-05 (+14.814913749694824219%) actual: +0.00016199937090277671814
[15]: +0.00030294340103864669800 (+2.5789010524749755859%) actual: +0.011746996082365512848
[16]: +0.00036497600376605987549 (+2.7727494239807128906%) actual: +0.013162964023649692535
I've tried nearly every combination of flags that I can think of to get this crap to line up correctly. Does anyone have an idea of what I'm missing?
Also do these flags only affect the *next* time that a value is printed out, or the next time that the stream buffer is flushed? If so that seems really stupid/inefficient to me. 
|
|
|
02-02-2006, 03:19 PM
|
#7
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
Try the following code to help you. I'm just playing around with the width manipulator.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
double n[3];
n[0] = 0.016414642333984375000;
n[1] = 0.0014805793762207031250;
n[2] = 6.4849853515625000000e-05;
for (int j = 0; j < 3; j++)
{
cout << "[" << j+8 << "]" << n[j] << endl;
}
for (int j = 0; j < 3; j++)
{
cout << "[" << setw(2) << j+8 << "]";
cout.width(20);
cout << n[j] << endl;
}
system("PAUSE");
return 0;
}
The output is:
Code:
[8]0.0164146
[9]0.00148058
[10]6.48499e-005
[ 8] 0.0164146
[ 9] 0.00148058
[10] 6.48499e-005
graeme.
|
|
|
02-02-2006, 04:50 PM
|
#8
|
Member
Registered: Dec 2005
Distribution: Fedora 4
Posts: 89
Rep:
|
who taught you guys how to program in C? just use the setw(width of field) before the field, and you will be golden. you need to include the iomanip header, and a code snippet of using some useful features:
Code:
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
void main()
{
float a, b, c, x, poly, last;
cout<<" This program computes and displays the value of"
<<"\na second order polynomial in the form: ax^2+bx+c"
<<endl
<<"-------------------------------------------------"
<<"\n\nEnter the value of a: ";//gather data from user
cin>>a;
cout<<"Enter the value of b: ";
cin>>b;
cout<<"Enter the value of c: ";
cin>>c;
cout<<"Enter the value of x: ";
cin>>x;
poly = a*pow(x,2.0)+b*x+c;//finds value of polynomial
cout<<"\n\na b c x polynomial value"//displays results
<<endl;
cout<<"----------------------------------------------------" <<endl
<<setiosflags(ios::fixed) <<setprecision(1) << a
<<setw(8)<<b
<<setw(9)<<c
<<setw(9)<<x
<<setw(15)<<setprecision(3)<<poly<<endl;//end of data display
cin>>last;//pauses so user can view results
}
and an output of that might be
Code:
This program computes and displays the value of
a second order polynomial in the form: ax^2+bx+c
-------------------------------------------------
Enter the value of a: 1
Enter the value of b: 2
Enter the value of c: -7
Enter the value of x: 6.25
a b c x polynomial value
----------------------------------------------------
1.0 2.0 -7.0 6.3 44.563
|
|
|
02-02-2006, 05:10 PM
|
#9
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
Quote:
who taught you guys how to program in C?
|
It's C++ not C that we are using and that was my point, either use the setw() or cout.width() before sending the variable to the output stream.
|
|
|
02-02-2006, 05:33 PM
|
#10
|
Member
Registered: Dec 2005
Distribution: Fedora 4
Posts: 89
Rep:
|
ya, i know, but the methods i showed can be used in C as well as C++, i do believe
|
|
|
02-02-2006, 05:42 PM
|
#11
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
The operator << is only used in C++. C uses the printf() function.
|
|
|
02-07-2006, 11:46 AM
|
#12
|
Member
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547
Original Poster
Rep:
|
I finally got things working as I wanted them too. The key was to trash that stupid width operator altogether. It was making my life hell. Here is the code and correct output:
Code:
// Set output flags: display floats in fixed-point, always show decimal point, precision of 10 digits
oout << fixed << showpoint << setprecision(10);
oout << "> Individual field errors" << endl;
for (uint32_t j = 0; j < data_manager_->dimensions; j++) {
oout << " [" << j << "]: "
<< run_results_[best_run_]->field_errors_[j]
<< " (" << run_results_[best_run_]->field_perc_errors_[j] << "%)\t"
<< "True value: " << full_field_sums_[j] << endl;
}
oout << endl;
Code:
> Individual field errors
[0]: 0.0283355713 (0.0509201437%) True value: 55.6470756531
[1]: -0.0023412704 (-0.0320433825%) True value: 7.3065648079
[2]: -0.0010080338 (-0.0094264681%) True value: 10.6936521530
[3]: 0.0000000000 (0.0000000000%) True value: 0.0000000000
[4]: 0.0002919952 (7.0615711212%) True value: 0.0041349898
[5]: -0.0014345646 (-0.0555693880%) True value: 2.5815734863
[6]: 0.0005369941 (5.6927275658%) True value: 0.0094329836
[7]: 0.0015468597 (0.0058440296%) True value: 26.4690608978
[8]: -0.0000130003 (-1.1393851042%) True value: 0.0011409954
[9]: 0.0029785633 (0.0754678771%) True value: 3.9467961788
[10]: -0.0001661927 (-0.1059862077%) True value: 0.1568059176
[11]: -0.0057635903 (-1.1124027967%) True value: 0.5181208253
[12]: 0.0012688339 (0.9677044749%) True value: 0.1311179101
[13]: 0.0000000000 (0.0000000000%) True value: 0.0000000000
[14]: 0.0000240001 (14.8149137497%) True value: 0.0001619994
[15]: 0.0002029426 (1.7276130915%) True value: 0.0117469961
[16]: 0.0006469777 (4.9151368141%) True value: 0.0131629640
It's not incredibly robust, because if the field argument gets too large, or I change the precision to 12 or so, things mess up just enough to screw up the tab spacing before True value. But this is good enough for me for now and I really spent more time working on getting this output formatted correctly. Yuck.
|
|
|
02-07-2006, 06:45 PM
|
#13
|
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065
Rep:
|
may help..
Code:
#include <iostream>
using namespace std;
int main(){
float arr[5] = {5.4533334, 7.44534333, 2.43434545454, 1.4343, 1.1};
float arr2[5] = {4.44444533334, 37.422224534333, 2.43, 133.2224343, 1.13333};
// use these vars to make things a little dynamic..
int precision=6, spacer=precision+4;
cout.precision(precision);
cout.fill(' ');
cout.flags(ios::left);
for(int i=0; i<5; ++i){
cout.width(3);
cout << i;
cout.width(spacer);
cout << arr[i];
cout.width(spacer);
cout << arr2[i];
cout.width(spacer);
cout << "otherstuff" << endl;
}
cin.get();
return 0;
}
i have not had to use any serious formatting for a while but i seem to remember doing somthing like this so that the fields are atleast a little dynamic..
|
|
|
02-14-2006, 12:51 PM
|
#14
|
Member
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547
Original Poster
Rep:
|
Oh, cool thanks!.  FYI for others, that code generates the following output:
Code:
0 5.45333 4.44445 otherstuff
1 7.44534 37.4222 otherstuff
2 2.43435 2.43 otherstuff
3 1.4343 133.222 otherstuff
4 1.1 1.13333 otherstuff
|
|
|
All times are GMT -5. The time now is 12:51 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|