#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    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
    Last edited by andy3109; May 16th, 2003 at 01:31 PM.
    hmmm...
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    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.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    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.
    hmmm...
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    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.
    hmmm...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    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.

IMN logo majestic logo threadwatch logo seochat tools logo