LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-18-2006, 11:57 AM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
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!
 
Old 01-18-2006, 12:32 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 01-18-2006, 01:21 PM   #3
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
Thanks, I appreciate it. I will look into those functions.
 
Old 01-18-2006, 05:30 PM   #4
SaintsOfTheDiamond
Member
 
Registered: Jan 2006
Location: Lexington, KY
Distribution: Arch and a little Slack
Posts: 139

Rep: Reputation: 15
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.
 
Old 01-18-2006, 05:37 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The following site may be of help if you are having any problems....
cout Man page

graeme
 
Old 02-02-2006, 02:45 PM   #6
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
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.
 
Old 02-02-2006, 03:19 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-02-2006, 04:50 PM   #8
airswit
Member
 
Registered: Dec 2005
Distribution: Fedora 4
Posts: 89

Rep: Reputation: 15
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
 
Old 02-02-2006, 05:10 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-02-2006, 05:33 PM   #10
airswit
Member
 
Registered: Dec 2005
Distribution: Fedora 4
Posts: 89

Rep: Reputation: 15
ya, i know, but the methods i showed can be used in C as well as C++, i do believe
 
Old 02-02-2006, 05:42 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The operator << is only used in C++. C uses the printf() function.
 
Old 02-07-2006, 11:46 AM   #12
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
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.
 
Old 02-07-2006, 06:45 PM   #13
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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..
 
Old 02-14-2006, 12:51 PM   #14
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No output through SPDIf, but analog works. How do I get output through SPDIF? seanwnz Linux - General 11 11-08-2007 11:24 AM
Trouble aligning heads on HP 3820 inkjet chowsapal Linux - Hardware 1 01-05-2006 08:59 AM
aligning torsmo kurrupt Linux - Software 1 06-28-2005 02:34 PM
Via AC'97 5.1 Optical Output or Audigy 4.1 Output Nza Fedora 3 06-01-2004 07:49 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration