how do you right align using <iostream.h>?
i've tried:
Code:...
cout.setf(ios::right);
...
Code:...
cout.setf(ios::right);
cout<<right;
...
Code:...
cout<<setiosflag(ios::right)<<...;
...
Printable View
how do you right align using <iostream.h>?
i've tried:
Code:...
cout.setf(ios::right);
...
Code:...
cout.setf(ios::right);
cout<<right;
...
Code:...
cout<<setiosflag(ios::right)<<...;
...
The simple manipulator is easiest:
Code:#include <iostream.h>
#include <iomanip.h>
int main()
{
cout<< right << setw(20) <<"Hello, world!"<<endl;
return 0;
}
Hi,
First, don't use header files with .h extensions. Use the equivalent header files, and read about the using directive here:
http://www.cplusplus.com/doc/ansi/hfiles.html
Second, output using cout is right aligned by default, but every field is the exact width of what you output, so the right side is ragged. All you have to do to right align output is set the width of all the fields the same:
#include<iomanip> //for setw()
#include<iostream> //for cout
using namespace std;
cout<<setw(25)<<"See Jane run."<<endl;
cout<<setw(25)<<"See Jane fall down."<<endl;
that would be helpful if i wasn't using an outdated compiler...Quote:
Originally posted by 7stud
First, don't use header files with .h extensions. Use the equivalent header files, and read about the using directive here:
http://www.cplusplus.com/doc/ansi/hfiles.html
i want a predefined right though...Quote:
Originally posted by 7stud
Second, output using cout is right aligned by default, but every field is the exact width of what you output, so the right side is ragged. All you have to do to right align output is set the width of all the fields the same:
thanks prelude... i'll try that later (i can't do it now - it's on a school compiler and the professor asked me how to do it, but none of the ways i tried helps...
You could always try
instead ofCode:cout.setw( 25 );
cout<<"bah"<<endl;
Code:cout<setw( 25 )<<"bah"<<endl;
but the setw(25) is only if the width of the console is 25 characters, i think
This might be old but I thought i'd help some of you out...
If you have "cout << right << num1 << num2 << num3 << num4;
I don't think num2,3,4, will have the right align. Doesn't seem to work that way. You have to separate:
Code:cout << fixed << right << setw(4) << setprecision(2) << num1;
cout << fixed << right << setw(4) << setprecision(2) << num2;