Hi,

I'm trying to output a list of dvd titles along with some of their details. The problem is that it seems I can't get them aligned all pretty well. So I upload the screen shot as well as the code generating it. Please help me out here. This is an old unsolved problem of mine.

cout << "ID: " << ID << "\t" << "Title: " << dvdItem << "\t\t\t\t"
			"Year: " << year << "\t" << "Want: " << want << "\t"
			"Have: " << have << "\t" << endl;

Take a look into:

#include <iomanip>

This allows you to manipulate your output. For example you can set the width of your output by using:

cout << setw(8) << variable;

Here the "variable" will be given a width of 8 characters. So, you can effectively make columns using setw().

You can also align left/right by using std::left or std::right. Use it the same way that I used setw() in my previous example.

Keep in mind that you have to do setw() for every single output, not just once.

cout << setw(8) "Hello" << setw(8) << "World" << endl;

However, std::right/left need only be done once.

actually, I used setw but still couldn't do it, but in a different way. So, i will check it out and let you know. Thanks a anyway.

replace those tabs with setw() as shown in previous post. If you want the text left-justified (meaning, padded with spaces on the right side of the text) then also add left]/b]

#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
    cout << left << setw(10) << "Hello"
        << left << setw(8) << "World\n";
}

There is also cout.width(...).

There is also cout.width(...).

cout.width() is not sticky. You need to call it for every object that gets printed, so code like this will only right justify the first string:

cout.width(30);
cout << "right justified\n"
     << "left justified\n";

To justify both strings, you need to split the output into two statements because cout.width() does not return a reference to the stream and cannot be chained:

cout.width(30);
cout << "right justified\n";
cout.width(30);
cout << "left justified\n";

The manipulator lets you do it all in one statement:

cout << setw(30) << "right justified\n"
     << setw(30) << "right justified\n";

So while cout.width() exists, I do not think there is much reason to use it unless you really hate including <iomanip> for the parameterized manipulators. ;)

commented: Agreed +1

Thank you guys all, and the good news is, I kind of did it.

After struggling a lot with setw, I managed to do it. It is solid or the best way to do it, I'm not quite sure. You could tell me if you think it's not convenient enough.

cout << setw(4) << "ID: " << setw(9) << ID << setw(7) << "Title: ";
		cout << setw(50) << dvdItem << setw(6) << "Year: " << setw(8) << year << 
		        setw(6) << "Want: " << setw(6) << want << setw(6) << "Have: " << setw(6) << have << endl;

As inconvenient as it may be.... that's the way to do it.

"So while cout.width() exists, I do not think there is much reason to
use it unless you really hate including <iomanip> for the parameterized
manipulators."

Its just easier for me to use cout.width, instead of including
iomanip, and using setw().

I tried to create hourglass with asterisk character using for loop... both decrement and increment works fine... now I'm stuck with text alignment where those asterisks is supposed to be printed justify/centered to bring an hourglass look...

any clue to get alignment works with loop result?
thx in advance..!
(sorry for my bad English)

here's my code:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
int bintang,baris;
  for(bintang=4; bintang>0; bintang--)
  {
    for(baris=0; baris<bintang; baris++) {
             cout.width(2);
             cout << "*";
             }
             cout << "\n";

             if (baris==1){
                           for(bintang=0; bintang<3; bintang++)
                              {
                                for(baris=-2; baris<bintang; baris++)
                                 {
                                   cout.width(2);
                                   cout << "*";
                                   }
                                   cout << "\n";
                         }
                         break;
             }
}
  cout << "\n";
  system("pause");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.