Formatting output into aligned columns

This is a discussion on Formatting output into aligned columns within the C++ Programming forums, part of the General Programming Boards category; I'm creating a program that gets a name and phone number from user input and puts it in a list. ...

  1. #1
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23

    Formatting output into aligned columns

    I'm creating a program that gets a name and phone number from user input and puts it in a list. It starts out with a blank list, numbered 1 through 20, that looks like this:
    1. (000) 000-0000
    2. (000) 000-0000
    3. (000) 000-0000
    4. (000) 000-0000
    5. (000) 000-0000
    etc.

    I can't figure out how to get the phone numbers all lined up vertically, because the length of each name the user enters will be different (this is why setw() doesn't work).

    Any ideas? Let me know if you want me to post the code I have so far.
    to Absent Toast

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >(this is why setw() doesn't work).
    Perhaps you need the left justify the names:
    Code:
    cout << left << setw(30) << name[i] << setw(20) << phone[i] << endl;

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    By standard, if you're gonna format some of the output, it's best if you format all of the output. It makes it easier if you want to go back and change things.

    Do as swoopy says. ...also giggle at his name.
    Sent from my iPadŽ

  4. #4
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Nope, doesn't work. Here's the code for the function that prints the list (the setw() parameters are fairly arbitrary):
    Code:
    void printMember(phoneNumber L[], int i)
    {
    	cout << i << ". " << left << L[i].name;
    
    	if (L[i].areaCode==0)
    		cout << setw(50) << "(000) 000-0000";
    	
    	else
    		cout << right << setw(20) << "(" << L[i].areaCode << ") " << L[i].exchange << "-" << L[i].number;
    	
    	cout << endl;
    }
    This is what I'm trying to get it to look like:
    Code:
    1.			(000) 000-0000
    2.			(000) 000-0000
    3. Mary Smith		(123) 456-7890
    4. John Anderson	(321) 789-4567
    5.			(000) 000-0000
    etc.
    Right now, Mary Smith's number is printed three spaces to the left because her name is three characters shorter than John Anderson's.
    to Absent Toast

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Why does your code say

    Code:
    cout << right << setw(20)
    when swoopy said

    Code:
    cout << left << setw(20)
    <---- Left
    Right ---->
    Sent from my iPadŽ

  6. #6
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Because otherwise it detaches the "(" and prints the line like this:
    Code:
    3. Mary Smith(        123) 456-7890
    ...and I don't know why. But I know that sticking the "right" in there fixes that problem. :-)

    Is there another way to fix this?
    to Absent Toast

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    cout << left << setw(30) << name[i] << "(" << L[i].areaCode << ") " << left 
         << setw(20) << phone[i] << endl;
    Sent from my iPadŽ

  8. #8
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Yay! I finally got it! Thanks for the help, it definitely led me in the right direction. Here's what I ended up with:
    Code:
    void printMember(phoneNumber L[], int i)
    {
    	if (i<10)
    	{
    		cout << " " << i << ". " << left << setw(NAMEMAX) << L[i].name;
    		if(L[i].areaCode==0)
    			cout << "(000) 000-0000" << endl;
    		else
        		cout << "(" << L[i].areaCode << ") " << L[i].exchange << "-"<< L[i].number << endl;
    	}
    	else
    	{
    		cout << i << ". " << left << setw(NAMEMAX) << L[i].name;
    		if(L[i].areaCode==0)
    			cout << "(000) 000-0000" << endl;
    		else 
        		cout << "(" << L[i].areaCode << ") " << L[i].exchange << "-"<< L[i].number << endl;
    	}
    }
    So basically, I was trying to format two parts of each line, and only needed to use one setw() to push the phone number down. NAMEMAX is a constant int that I already defined, it represents the maximum size of the name you can enter. The first if-else statements puts a space before the number of the listing so it's lined up properly with the double-digit listings. Then, if a phone number hasn't been entered yet for a particular listing, it'll just display (000) 000-0000 (this is what my instructor said it should do).
    I'm starting to realize that it's the things that seem the easiest at first which end up being the most complicated and time-consuming.
    to Absent Toast

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Do as swoopy says. ...also giggle at his name.
    And I love your avatar Sly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. formatting output
    By kisiellll in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 04:53 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Output Columns form a file
    By Cdrwolfe in forum Tech Board
    Replies: 2
    Last Post: 03-22-2006, 05:12 AM
  4. formatting output
    By z.tron in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2002, 06:11 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 07:50 PM

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