Aligning output using setw

I'm trying to align my output but I can't get it to look perfect.

This is how it looks like in my program:

1
2
3
4
Hitch                               98%     FRESH
Happy Gilmore                       99%     FRESH
Transformers 2                      100%     FRESH
SpongeBob                           10%     ROTTEN


And this is how I would like it to look:
1
2
3
4
Hitch                               98%      FRESH
Happy Gilmore                       99%      FRESH
Transformers 2                     100%      FRESH
SpongeBob                           10%     ROTTEN


Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
void PrintAll(ostream& out, const string titles[], const int ratings[], int count)
{
	WriteLine(out, '=', 50);
	out << "PRINT ALL" << endl;
	WriteLine(out, '-', 50);
    
	for(int i = 0; i < count; i++)
	{
        out << setw(37) << left << titles[i]
            << ratings[i] << setw(6) << left <<  "%"
            << RatingToString(ratings, i) << endl;
	}
}


Any help would be greatly appreciated. Thank you.
Last edited on
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
void PrintAll(ostream& out, const string titles[], const int ratings[], int count)
{
	WriteLine(out, '=', 50);
	out << "PRINT ALL" << endl;
	WriteLine(out, '-', 50);
    
	for(int i = 0; i < count; i++)
	{
        out << setw(37) << left << titles[i]
            << right << setw(6) << ratings[i] <<  "%";
            << setw(10) << RatingToString(ratings, i) << endl;
	}
}
@ajh32 That did it! Thank you very much.

Do you mind explaining how that fixed it though? I have a hard time understanding how left and right work, and then knowing if to put them before or after setw().
You need to set the attributes to the ostream, that you want applied before you send the string | int | etc... to the ostream, othwerwise the string etc won't exhibit the attribute you intended it will use the attributes that have already been set.

Does that make any sense to you?
Last edited on
@ajh32 Yes, I see what you mean.

But does it matter if you have right or left before setw() compare to having setw() then right or left?
But does it matter if you have right or left before setw() compare to having setw() then right or left?


No
Topic archived. No new replies allowed.