Centering

How do I center things?

like:

Company
Week of ____
Name Hours Rate Base Overtime Gross Fedtax Netpay
x x x x x x x x

Comments

  • : How do I center things?
    :
    : like:
    [code]
    : Company
    : Week of ____
    : Name Hours Rate Base Overtime Gross Fedtax Netpay
    : x x x x x x x x
    :
    [/code]

    As long as you're working with a fixed font as in the console, you can simply find the difference between the string length and the longest line length, then divide that difference by 2. Pad the front of the string with that result of spaces.

    For example:

    The normal console line length is 80 characters
    Company is 7 characters
    Week of ____ is 12 characters
    Name Hours Rate Base Overtime Gross Fedtax Netpay is 48 characters

    80 - 7 = 73 / 2 = 36
    80 - 12 = 68 / 2 = 34
    80 - 48 = 32 /2 = 16

    Place 36 spaces before Company, 34 before Week..., etc.

    The rest is a bit more complicated, but you should get the idea.

    Take Care,
    Ed

  • : : How do I center things?
    : :
    : : like:
    : [code]:
    : : Company
    : : Week of ____
    : : Name Hours Rate Base Overtime Gross Fedtax Netpay
    : : x x x x x x x x
    : :
    : [/code]:
    :
    : As long as you're working with a fixed font as in the console, you
    : can simply find the difference between the string length and the
    : longest line length, then divide that difference by 2. Pad the
    : front of the string with that result of spaces.
    :
    : For example:
    :
    : The normal console line length is 80 characters
    : Company is 7 characters
    : Week of ____ is 12 characters
    : Name Hours Rate Base Overtime Gross Fedtax Netpay is 48 characters
    :
    : 80 - 7 = 73 / 2 = 36
    : 80 - 12 = 68 / 2 = 34
    : 80 - 48 = 32 /2 = 16
    :
    : Place 36 spaces before Company, 34 before Week..., etc.
    :
    : The rest is a bit more complicated, but you should get the idea.
    :
    : Take Care,
    : Ed
    :
    :
    [color=Blue]Or you can use simple old C language:
    [/color]
    [code]
    int main ()
    {
    char* s1 = "ABC";
    char* s2 = "ABCD";
    char* s3 = "ABCDE";
    printf ("%8s%8s%8s", s1, s2, s3);
    getchar ();
    return 0;
    }
    [/code]
  • : How do I center things?
    :
    : like:
    :
    : Company
    : Week of ____
    : Name Hours Rate Base Overtime Gross Fedtax Netpay
    : x x x x x x x x
    :
    printf() allows you to pass a field width.

    char *name = "Fred";
    float hours = 2.12;
    float rate = 1.4;

    printf("%16s %6.2f %6g
    ", name, hours, rate);
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories