Thread: formatting output

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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,336
    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
    28,413
    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';
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 06:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM