Having some trouble formatting output

My program has a few values it out puts on different lines using a loop. Basically each line needs to look like:

1
2
3
4
Name                Goals      Assists      Points     Rating
John Smith            7          8            15         +2 
John Doe              2          4            6          +1          
Jonathon Jones        0          0            0           0


Instead my output looks like:
1
2
3
4
Name                Goals      Assists      Points     Rating
John Smith            7          8            15         +2 
John Doe            2          4            6          +1          
Jonathon Jones            0          0            0           0


I can use setw and get even spacing on the numbers but because the names are different lengths setw is not an option.

Here is my code for the output:
1
2
3
4
	for(i=1; i<=numPlayers; i++)
	{	 
		cout<<players[i].name<<std::setw(12) <<players[i].goals<<std::setw(8)<<players[i].assists<<std::setw(8)<<points[i]<<std::setw(8)<<players[i].rating<<endl;
	}

but because the names are different lengths setw is not an option

On the contrary. Because the names are different lengths, you need to use setw() for the names too.
Im using a loop to output all the data so i can't use it. Also the names are char arrays (not by choice) so I cant determine the length.
Whether or not you use a loop doesn't affect the problem.
You could find the length of the name if you needed to, using strlen().

But none of that really matters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

using namespace std; 

int main()
{
    const char * names[3] = {
        "John Smith", 
        "John Doe",
        "Jonathon Jones"
    };

    for (int i=0; i<3; i++)
    {
        cout << setw(20) << left << names[i] << setw(4) << right << i << endl;
    }

    return 0;
}

Output:
John Smith             0
John Doe               1
Jonathon Jones         2

Last edited on
Got it to work! Thanks for the help.
Topic archived. No new replies allowed.