formatting output

This is a discussion on formatting output within the C++ Programming forums, part of the General Programming Boards category; I am trying to line everything up. Problem is it counts how many characters the name is and setw how ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    formatting output

    I am trying to line everything up. Problem is it counts how many characters the name is and setw how far past that name. So all the names are different lengths. I cant get everythnig to line up.. I have tried <<left and << right and << setw

    what i need the putput to look like is this:
    Code:
    name                      location           acres rating
    ----                      --------           ----- ------
    Cooper Mountain           Willamette Valley    100     47
    Del Rio                   Bear Creek Valley    200     37
    Duck Pond Cellars         Willamette Valley    845     70
    Gallo                     Napa Valley          200     25
    LongSword                 Applegate Valley      16     85
    Lopez Island Vinyard      San Juan Islands       7     95
    Weisinger's of Ashland    Bear Creek Valley      6     83

    Code:
    void list::displayByName(ostream& out) const
    {
    
    	winery *wineryPtr;
    
    	wineryPtr->displayHeadings(out);
    
    	node *curr  = headByName;
    
    	while ( curr )
    	{
         
    		out  << curr->item.getName() << setw(25)
    			<< curr->item.getLocation() << setw(15) 
    			<< curr->item.getAcres() << setw(15)
    			<< curr->item.getRating()
    			<< endl;
    
    		curr = curr->nextByName;
    	}
    
    }
    problem is what i am getting is:
    Code:
    
    CS260 - Lab1 - Corey Mattis
    
    
    +++ list by name
    
    Name                 Location  Rating  Acre
    -----                 ------------------
    Cooper Mountain        Willamette Valley            100             47
    Del Rio        Bear Creek Valley            200             37
    Duck Pond Cellars        Willamette Valley            845             70
    Gallo              Napa Valley            200             25
    LongSword         Applegate Valley             16             85
    Lopez Island Vinyard         San Juan Islands              7             95
    Weisinger's of Ashland        Bear Creek Valley              6             83
    I did have some success with getting the lengths of the name s and using that in setw
    However, it didnt line up like i wanted.
    Code:
    string name = curr->item.getName();
    size_t fieldWidth = name.size();
           
    out  << curr->item.getName() << setw(-fieldWidth + 45)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    setw should work, but you will need to set it before printing each name, location, etc, if I remember correctly. Plus you'll need to determine the longest name, location, etc if they do not have a pre-defined maximum length.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    setw is one-time-use, as laserlight mentioned, so you'll need it before each print. Also you are not passing the number of spaces, but the actual field width itself to setw (I'm not sure what exactly 45-fieldWidth is supposed to represent, but I'm not confident in it)..

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Well, if i do this
    Code:
    void list::displayByName(ostream& out) const
    {
    	winery *wineryPtr;
    	wineryPtr->displayHeadings(out);
    
    	node* curr  = headByName;
    	
    
    
    	while ( curr )
    	{
    	   
           
    		out << setw(20) << curr->item.getName() 
    			<< curr->item.getLocation() << setw(15) 
    			<< curr->item.getAcres() << setw(15)
    			<< curr->item.getRating()
    			<< endl;
    
    		curr = curr->nextByName;
    	}
    my output is this..


    Code:
    
    CS260 - Lab1 - Corey Mattis
    
    
    +++ list by name
    
    Name                 Location  Rating  Acre
    -----                 ------------------
         Cooper MountainWillamette Valley            100             47
                 Del RioBear Creek Valley            200             37
       Duck Pond CellarsWillamette Valley            845             70
                   GalloNapa Valley            200             25
               LongSwordApplegate Valley             16             85
    Lopez Island VinyardSan Juan Islands              7             95
    Weisinger's of AshlandBear Creek Valley              6             83

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Really? Did some weird copy paste thing happen, because the numbers should be lined up (except the last, because Weisinger's of Ashland doesn't fit in the field). But how is that different than what you expected?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Try something like this:
    Code:
    out << left
        << setw(25) << curr->item.getName()     << ' '
        << setw(18) << curr->item.getLocation() << ' '
        << right
        << setw(5)  << curr->item.getAcres()    << ' '
        << setw(6)  << curr->item.getRating()   << '\n';
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    laserlight, you are brilliant.. worked perfectly.. Thank you
    I had tried using left on the same line as setw, but it didnt work like i wanted.
    Last edited by mrsirpoopsalot; 10-07-2009 at 02:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21