|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
can iomanip(setw) align left?
I have two columns and I have setw(20) to each of them like this:
files.setf(ios::fixed); files << "\n" << setw(20) << names; files.setf(ios::fixed); files << setw(20) << p; files is a stream that is writing to a ".txt" file. the "ios::right" aligns them to the right of the 20 spaces and so does "fixed." Is there anyway to have them aligned left of the 20 spaces I have reserved for text? Thanks. By the way ios::left doesn't work. -andy
__________________
hmmm... Last edited by andy3109 : May 16th, 2003 at 01:31 PM. |
|
#2
|
||||
|
||||
|
Why not omit the setw() when attempting to print the integer or whatever. If you don't use setw, it automatically left-aligns by default.
|
|
#3
|
||||
|
||||
|
then the columns wouldn't be even. If one row of the column has 15 characters and the next has 3 characters then it will push the second column over (15-3) 12 characters farther then the second row.
EX: Name.....................Points John Doe...........................412 jo...............................315 -andy Last edited by andy3109 : May 16th, 2003 at 01:49 PM. |
|
#4
|
||||
|
||||
|
NM..found the solution (probably not the best way but I found one). Thanks guys!
-andy Last edited by andy3109 : May 16th, 2003 at 06:02 PM. |
|
#5
|
|||
|
|||
|
Actually, the default is right justification, but since each field is exactly the size of what's being output, the right side will appear ragged(as I'm sure Scorpions4ever knows). You can prove that by just setting the field width to something uniform like 8:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int num1 =100;
int num2 =45686;
cout<<setw(8)<<num1<<endl;
cout<<setw(8)<<num2<<endl;
return 0;
}
To left justify, use the "left" output manipulator: Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int num1 =100;
int num2 =45686;
int num3 =5678;
int num4 =8483444;
cout<<left
<<setw(8)<<num1
<<setw(8)<<num2
<<endl
<<setw(8)<<num3
<<setw(8)<<num4
<<endl;
return 0;
}
Edit: Ooops! You want to do file output. You can use your ofstream object name instead of cout in the code above, or if you want to use setf() to change the mode, ios::left does work: Code:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
ofstream outFile;
outFile.open("C:\\TestData\\output.txt");
int num1 =100;
int num2 =45686;
int num3 =5678;
int num4 =8483444;
int x = 20;
outFile<<setw(x)<<num1
<<setw(x)<<num2
<<endl
<<setw(x)<<num3
<<setw(x)<<num4
<<endl;
outFile.setf(ios::left);
outFile <<setw(x)<<num1
<<setw(x)<<num2
<<endl
<<setw(x)<<num3
<<setw(x)<<num4
<<endl;
return 0;
}
Last edited by 7stud : May 17th, 2003 at 05:41 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > can iomanip(setw) align left? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|