ProgrammingThis 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.
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.
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!
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:
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.
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
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:
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.
#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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.