Printing a table of strings and data?

This is a discussion on Printing a table of strings and data? within the C Programming forums, part of the General Programming Boards category; I would like to output my data in columns with headers. I looked at some printf escape sequences, but haven't ...

  1. #1
    Registered User Godfearn's Avatar
    Join Date
    Dec 2011
    Posts
    5

    Printing a table of strings and data?

    I would like to output my data in columns with headers.
    I looked at some printf escape sequences, but haven't found anything to satisfy me.Is there any way to ensure the data lines up with the headers?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float f,c,t1,t2,duty,period;
        int r1,r2;
        printf("Enter value for R1(ohms): ");
        scanf("%d", &r1);
        printf("\nEnter value for R2(ohms): ");
        scanf("%d", &r2);
        printf("\nEnter value for C(uF): ");
        scanf("%f", &c);
            c= c*1E-6;
            f = 1/(.693*(r1+(2*r2))*c); /* freq(hz) = 1/ .693*(r1+(2*r2))*c */
            period = 1/f;
            t1 = .693*(r1+r2)*c;
            t2 = .693*r2*c;
            duty = (t1/period)*100;
        printf("\nFreq     Period      T1       T2          Duty Cycle");
        printf("\n%fHz     fsec%   %f     %f      %f\%%", f, period,t1,t2,duty);    
        return 0;    
    }
    Name:  astable555.jpg
Views: 614
Size:  45.3 KB

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    You may want to look into using the width and precision specifiers in your printf() format string.

    Jim

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What Jim said... or, alternatively use \t tab characters...
    Code:
    printf("Col1\tCol2\t"...
    Jims is better, mine is easier... Your choice.

  4. #4
    Registered User Godfearn's Avatar
    Join Date
    Dec 2011
    Posts
    5

    solution

    I tried width specifiers, but had a headache trying to match the strings and data. So I used a character array for the column heading, then matched the data with the width specifier. I have to say I'm happy with the output.
    Thanks for the help.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float f,c,t1,t2,duty,period;
        int r1=8200,r2=68000;
        char heading[100] = "Freq(Hz)     Period     T1(high)     T2(low)     Duty Cycle";
        
            c= .000001;
            f = 1/(.693*(r1+(2*r2))*c);  /*freq(hz) = 1/ .693*(r1+(2*r2))*c */
            period = 1/f;
            t1 = .693*(r1+r2)*c;
            t2 = .693*r2*c;
            duty = (t1/period)*100;        
            printf("%s", heading);
            printf("\n");
        
        printf("%-12.2f %-10.3f %-11.3f  %-10.3f  %.3f\%%", f, period,t1,t2,duty);   
        
        return 0;    
    }

    Name:  table.jpg
Views: 557
Size:  35.8 KB

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You can copy from the Windows console by right clicking the top of the window > clicking "Edit>Mark" > boxing the text that you wish to copy and then right clicking. Any text within that box will now be on your clipboard.

    The way you have done it is perfectly fine for anyone trying to help you though, just trying to save you some time for next time.

  6. #6
    Registered User Godfearn's Avatar
    Join Date
    Dec 2011
    Posts
    5
    ohh ya, I forgot about that. I think I've used printscn so often it may take me less time =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a table-like text
    By C_ntua in forum C# Programming
    Replies: 2
    Last Post: 11-16-2010, 04:35 PM
  2. Storing and printing an array table
    By jaja009 in forum C Programming
    Replies: 3
    Last Post: 03-12-2010, 05:13 AM
  3. Printing a colored table , how to ?
    By GSalah in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2006, 07:59 AM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 12:04 PM
  5. Printing out a data table in different sequences
    By TheDudeAbides in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 01:06 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21