setw gives extra space after first loop

425 Views Asked by At

How do I stop setw() from creating space after an endl? My first loop here which displays my column headers works fine, but on my second loop (which displays data), I get a large blank space before it shows the first line. I have tried clearing the buffer and putting extra "endl"s in.

for (int i = 0; i < airports.size(); i++)
{

    cout << airports[i] <<setw(10);
}
cout << endl;

for (int i = 0; i < numAirprts; i++)
{
    for (int j = 0; j < numAirprts; j++)
    {
        cout << i;
        cout << setw(4);

        for (int k = 0; k < milesCost; k++)
        {
            cout << costArray[i][j][k] << setw(2) <<" ";
        }

    }
    cout << endl;
}

and this is an example of the output:

DEN       LAX        LV       PHX       SFA       SFO       SJC       SLC

         0  -1  -1  0 844  69  0  -1  -1  0  -1  -1  01026  72  0  -1  -1  0  -1  -1  0  -1  -1
1  -1  -1  1  -1  -1  1 231  23  1  -1  -1  1  -1  -1  1 344  39  1  -1  -1  1 581  57
2  -1  -1  2  -1  -1  2  -1  -1  2 256  21  2  -1  -1  2  -1  -1  2  -1  -1  2 362  29
3 586  65  3  -1  -1  3  -1  -1  3  -1  -1  3  -1  -1  3  -1  -1  3  -1  -1  3  -1  -1
4  -1  -1  4  -1  -1  4  -1  -1  4  -1  -1  4  -1  -1  4 679  67  4  -1  -1  4 700  59
5  -1  -1  5  -1  -1  5 420  39  5  -1  -1  5  -1  -1  5  -1  -1  5  -1  -1  5 605  19
6  -1  -1  6  -1  -1  6  -1  -1  6  -1  -1  6  -1  -1  6  51  19  6  -1  -1  6  -1  -1
7 379  49  7  -1  -1  7  -1  -1  7  -1  -1  7  -1  -1  7  -1  -1  7 585  29  7  -1  -1
1

There are 1 best solutions below

0
On

You could reset setw before endling, something like:

cout << setw(0) << endl;