Thread: Lining up decimals & text - code included inside.

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Thumbs up Lining up decimals & text - code included inside.

    Hello, I am having troubles lining up decimals and text in my output.

    This is what my output consists of

    Code:
    No. Last Name     Gross     LTB     EI     CPP     FED TAX     TOT DED     NET PAY
    111 Lastname01       288.40         4.00         4.04         4.61        67.83        80.48       207.92
    222 Lastname02      4807.69        96.00        11.80        11.20      1942.10      2061.10      2746.60
    333 Lastname03       405.51         8.00         5.68         6.49        97.53       117.69       287.82
    444 Name     11057.69       100.00        11.80        11.20      4606.47      4729.47      6328.22
    555 Name       339.68         6.00         4.76         5.43        79.89        96.08       243.60
    666 Name       353.32         6.00         4.95         5.65        83.10        99.70       253.62
    777 Name       439.20         8.00         6.15         7.03       108.92       130.09       309.11
    888 Name       321.20         6.00         4.50         5.14        75.55        91.18       230.02
    999 Name       321.92         6.00         4.51         5.15        75.72        91.37       230.55
    As you can see its fairly messy.

    Here is the code I have written.

    Code:
    fprintf(outputfile, "No. Last Name     Gross     LTB     EI     CPP     FED TAX     TOT DED     NET PAY");
    and

    Code:
    fprintf(outputfile, "\n%d %s %12.2lf %12.2lf %12.2lf %12.2lf %12.2lf %12.2lf %12.2lf", empnum, lname, grosspay, ltb, 
    																 employment_ins, can_pen_plan, fed_tax,
    																 ltb + employment_ins + can_pen_plan + fed_tax,
    																 grosspay - (ltb + employment_ins + can_pen_plan + fed_tax));

    I would appreciate any help towards fixing up and formatting this text. Thanks in advance.

    As well, its my first post on the forums here

    PS: I hope this is all the required code needed, if more is needed - let me know.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Add a width specifier for your string specifier.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    That didn't quite work.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Really? It works for me.
    Code:
    #include <stdio.h>
    struct foo
    {
            char *word;
            float ta;
            float me;
            float hom3z;
            float yo;
    } peace[] =
    {
            { "out", 1.337, 1.2, 2.333, 4.444 },
            { "w0rd", 2.5, 6666.9, 867.5309, 0.1812 },
            { "give", 0.0, 2.222, 12.9, 999.0 },
            { "propz", 1.1, 3.333, 23141.2, 12.0 },
            { "yo", 0.0, 0.0, 10.01, 9.9 }
    };
    
    int main( void )
    {
            int x;
            for( x = 0; x < sizeof peace / sizeof peace[0]; x++ )
            {
                    printf("%-20s %-15.5f %-15.5f %-15.5f %-15.5f\n",
                                    peace[x].word,  peace[x].ta, peace[x].me,
                                    peace[x].hom3z, peace[x].yo );
            }
            return 0;
    }
    Murtaugh: Word, Riggs!

    Riggs: Word Rog.

    Murtaugh: What are we talking about?

    Riggs: Word, four letters, starts with W, OR in the middle, D on the end. Word.

    Murtaugh: Oh, that word.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    I have no idea what that code does. I was only recently taught to use things like this
    Code:
    %12.2lf
    .

  6. #6
    The format tags follow this prototype:
    %[flags][width][.precision][modifiers]type

    You can learn about these options here.

    For a brief summary:

    The width is 12 representing a minimum number of characters to be printed. If the value to be printed is shorter than this number the result is padded with blanks.

    Your precision is .2 representing number of digits to be printed after the decimal point. [if nothing specified default is 6].

    Your modifier is l representing the argument interpreted as long int (interger types) or double (floating point types).

    Your type is f representing a decimal floating point.


    - Stack Overflow
    Last edited by Stack Overflow; 11-26-2004 at 11:08 PM. Reason: Fixed Typos
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Ok, so I need to change the "%12.2lf" to another number?

    That doesnt fix anything.

  8. #8
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    How about this:

    Code:
    int main( void )
    {
            int x;
            for( x = 0; x < sizeof peace / sizeof peace[0]; x++ )
            {
                    printf("%20s %15.5f %15.5f %15.5f %15.5f\n",
                                    peace[x].word,  peace[x].ta, peace[x].me,
                                    peace[x].hom3z, peace[x].yo );
            }
    
      system("PAUSE");	
      return 0;
    }
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 01-10-2007, 06:53 AM
  2. printf+gets inside of code
    By tomas.ra in forum C Programming
    Replies: 7
    Last Post: 11-01-2005, 10:58 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM
  5. Merging files (code included)
    By TankCDR in forum C Programming
    Replies: 3
    Last Post: 10-28-2001, 11:06 AM