The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
can iomanip(setw) align left?
Discuss can iomanip(setw) align left? in the C Programming forum on Dev Shed. can iomanip(setw) align left? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 16th, 2003, 01:28 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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.
|

May 16th, 2003, 01:42 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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.
|

May 16th, 2003, 01:45 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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.
|

May 16th, 2003, 05:51 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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.
|

May 17th, 2003, 04:59 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|