Thread: Formatting with Printf

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Formatting with Printf

    I am using the for loop below to print records from a database table to the users screen. I am having problems displaying the results in a lined up view. The data types are also below with a screen shot of how messy it currently looks. Any help is greatly appreciated getting this in a better formatted view.

    Code:
    /* PRINT TO SCREEN */
    /*READ ALL RECORDS FOUND IN TABLE - TOTAL IS STORED IN variable COUNT*/
     for( x = 0; x < row_count; x++ )
      {
    printf("COST CENTER:  LOCATION:   DESCRIPTION:        GROUP NUMBER:\n");
    printf("%-14d         %-11s       %-20s               %s\n",  cc_lkup2[x].cc,  cc_lkup2[x].cc_sn,  cc_lkup2[x].cc_desc,  cc_lkup2[x].cc_grp);
      }
    
    return (0);
    }
    Code:
    Column Name            Type       Length Nulls Defaults
    cc                               integer         2    no      no     
    cc_sn                         varchar        10   no     yes     
    cc_desc                      varchar        40   no     yes     
    cc_grp                        varchar        40   no     yes
    Screenshot of current output:

    COST CENTER: LOCATION: DESCRIPTION: GROUP NUMBER:
    249 BLDG 30 30 WASTE TREATMENT BUILDING
    COST CENTER: LOCATION: DESCRIPTION: GROUP NUMBER:
    518 127 MILL 127 TEMPER MILL ROLLING
    COST CENTER: LOCATION: DESCRIPTION: GROUP NUMBER:
    808 CHROMAGRAP OUTSIDE PROCESSOR - CHROMAGRAPHICS OUTSIDE PROCESS
    COST CENTER: LOCATION: DESCRIPTION: GROUP NUMBER:
    812 LYONS SLIT OUTSIDE PROCESSOR - LYONS SLIT OUTSIDE PROCESS
    COST CENTER: LOCATION: DESCRIPTION: GROUP NUMBER:
    526 NO 2 NORM NO. 2 NORMALIZER ANNEALING
    COST CENTER: LOCATION: DESCRIPTION: GROUP NUMBER:
    591 LOAD/SHIP LOADING & SHIPPING INSP., SHIP., STORAGE & DISTRIBUTION

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    That's not a screen shot and dumping "output" into an unformatted (no code tags) portion of your post doesn't really help us to truly see that the output is indeed messy.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cjohnman View Post
    Code:
    COST CENTER:  LOCATION:   DESCRIPTION:        GROUP NUMBER:
    249                    BLDG 30           30 WASTE TREATMENT                 BUILDING
    COST CENTER:  LOCATION:   DESCRIPTION:        GROUP NUMBER:
    518                    127 MILL          127 TEMPER MILL                    ROLLING
    COST CENTER:  LOCATION:   DESCRIPTION:        GROUP NUMBER:
    808                    CHROMAGRAP        OUTSIDE PROCESSOR - CHROMAGRAPHICS               OUTSIDE PROCESS
    COST CENTER:  LOCATION:   DESCRIPTION:        GROUP NUMBER:
    812                    LYONS SLIT        OUTSIDE PROCESSOR - LYONS SLIT               OUTSIDE PROCESS
    COST CENTER:  LOCATION:   DESCRIPTION:        GROUP NUMBER:
    526                    NO 2 NORM         NO. 2 NORMALIZER                   ANNEALING
    COST CENTER:  LOCATION:   DESCRIPTION:        GROUP NUMBER:
    591                    LOAD/SHIP         LOADING & SHIPPING                 INSP., SHIP., STORAGE & DISTRIBUTION

    I notice that cc_desc is varchar 40, but you allow 20 spaces for it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Output Question

    I will change that to 40. Thanks for pointing that out. To rephrase my question a little. I want to print the first column with a width of 10 (due to heading), second column start one space after first and contain width of 10, third column start one space after second and have a width of 40, fourth column start one space after third and have width of 40. I hope that makes sense. Thanks.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Oh, and:
    Code:
    printf("%-14d         %-11s       %-20s               %s\n",  cc_lkup2[x].cc,  cc_lkup2[x].cc_sn,  cc_lkup2[x].cc_desc,  cc_lkup2[x].cc_grp);
    should probably be:
    Code:
    printf("%-13d %-10s %-19s %s\n",  cc_lkup2[x].cc,  cc_lkup2[x].cc_sn,  cc_lkup2[x].cc_desc,  cc_lkup2[x].cc_grp);
    Your spaces in the printf is being printed above and beyond the spaces needed to do the formatting.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Thanks for your input matsp - Code is updated and working great now

    Code:
    /* PRINT TO SCREEN */
    /*READ ALL RECORDS FOUND IN TABLE - TOTAL IS STORED IN variable COUNT*/
     for( x = 0; x < row_count; x++ )
      {
    printf("COST CENTER: LOCATION:  DESCRIPTION:                             GROUP NUMBER:\n");
    printf("%-12d %-10s %-40s %s\n",  cc_lkup2[x].cc,  cc_lkup2[x].cc_sn,  cc_lkup2[x].cc_desc,  cc_lkup2[x].cc_grp);
      }
    
    return (0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM