Thread: Formatting problem

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Formatting problem

    Code:
    #include <stdio.h>
    
    int main()
    {
        char date[] = "02/16/05";
        char quantity[] = "10";
        char description[] = "t-shirt";
        char price[] = "29.99";
    
        printf("Date\t\tQty\tDescription\t\t\tPrice\n");
        printf("----\t\t---\t-----------\t\t\t-----\n");
        printf("%s\t\t%s\t%s\t\t\t%s\n", date, quantity, description, price);
    
        return 0;
    }
    Here's my output:
    Code:
    Date            Qty     Description                     Price
    ----            ---     -----------                     -----
    02/16/05                10      t-shirt                 29.99
    Press any key to continue
    Why does it happen?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Why does it happen?

    Tabs are a different spacing than you expect. Try using width specifiers.
    Code:
    #include <stdio.h>
    
    int main()
    {
        char date[] = "02/16/05";
        char quantity[] = "10";
        char description[] = "t-shirt";
        char price[] = "29.99";
    
        printf("%-10s %-3s %-20s %-5s\n", "Date", "Qty", "Description", "Price");
        printf("%-10s %-3s %-20s %-5s\n", "----", "---", "-----------", "-----");
        printf("%-10s %-3s %-20s %-5s\n", date, quantity, description, price);
    
        return 0;
    }
    
    /* my output
    Date       Qty Description          Price
    ----       --- -----------          -----
    02/16/05   10  t-shirt              29.99
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why does it happen?
    Tabs are foogly and best avoided for any advanced formatting.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    it is a problem with your spacing, nothing else

    try this..
    Code:
     #include <stdio.h>
     #include <stdlib.h>
    int main()
    {
        char date[] = "02/16/05";
        char quantity[] = "10";
        char description[] = "t-shirt";
        char price[] = "29.99";
    
        printf("Date    \t\tQty\tDescription\t\t\tPrice\n");
        printf("----    \t\t---\t-----------\t\t\t-----\n");
        printf("%s\t\t%s\t%s   \t\t\t%s\n", date, quantity, description, price);
        system("PAUSE");
        return 0;
    }
    i guess this should work..
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Formatting problem with dates.
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2002, 09:35 AM