-
aligning a table?
I'm having trouble aligning a table
Code:
/* unaligned table */
City | x coord | y coord
---------------------+---------+---------
Nowhere 28.80 17.60
Somewhere 5.60 79.12
Whoknows 55.30 49.90
Elsewhere 22.22 11.11
/* aligned table */
City | x coord | y coord
---------------------+---------+---------
Elsewhere 22.22 11.11
Nowhere 28.80 17.60
Whoknows 55.30 49.90
Somewhere 5.60 79.12
The tricky part is, the file is dynamic so if i change the contents the alignment is broken:
Code:
/* unaligned table */
City | x coord | y coord
---------------------+---------+---------
city1 4.40 7.70
city2 3.30 8.80
/* aligned table */
City | x coord | y coord
---------------------+---------+---------
city1 4.40 7.70
city2 3.30 8.80
Here's what my code looks like for the unaligned version:
Code:
printf("City | x coord | y coord");
printf("\n---------------+---------+---------\n");
printf("%9s %15.2f %9.2f\n", startptr->cityname,startptr->xcp, start-ptr>yco);
-
Just use %-9s, then? It will left justify it, using a minimum of 9 characters.
-
thanks. Hmm this is strange:
Code:
City | x coord | y coord
---------------------+---------+---------
city1 4.40 7.70
city2 3.30 8.80
works fine
this:
Code:
City | x coord | y coord
---------------------+---------+---------
Nowhere 28.80 17.60
Somewhere 5.60 79.12
Whoknows 55.30 49.90
Elsewhere 22.22 11.11
doesn't...
-
Works for me. My code:
Code:
#include <stdio.h>
struct foo {
char *bar;
double v1, v2;
} baz[] = {
{"Nowhere", 28.80, 17.60},
{"Somewhere", 5.6, 79.12},
{"Whoknows",55.3, 49.9},
{"Elsewhere",22.22,11.11},
{NULL,0,0}
};
int main(void)
{
struct foo *quux = baz;
while (quux->bar)
{
printf("%-9s %15.2f %9.2f\n", quux->bar, quux->v1, quux->v2);
quux++;
}
return 0;
}
My output:
Code:
Nowhere 28.80 17.60
Somewhere 5.60 79.12
Whoknows 55.30 49.90
Elsewhere 22.22 11.11
-
yup you're right. But
Code:
City | x coord | y coord
---------------------+---------+--------
Elsewhere 22.22 11.11
Sidknee 147.00 34.00
Gotham 83.70 26.80
Somewhere 5.60 79.12
Whoknows 55.30 49.90
Trantor 0.00 0.00
Metropolis 12.56 12.09
Smallville 16.86 5.20
Nowhere 28.80 17.60
no idea why it's not working. It's basically the same as above with a few more stuff added.
-
ok changed the min character length to 10 and it works now thanks.