C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 16th, 2003, 01:28 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
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.

Reply With Quote
  #2  
Old May 16th, 2003, 01:42 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,389 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 1 m 11 sec
Reputation Power: 4080
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.

Reply With Quote
  #3  
Old May 16th, 2003, 01:45 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
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.

Reply With Quote
  #4  
Old May 16th, 2003, 05:51 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
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.

Reply With Quote
  #5  
Old May 17th, 2003, 04:59 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > can iomanip(setw) align left?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap