How do I fit these columns?

Thread Starter

asdf arfw

Joined Sep 9, 2017
43
I don't know how to explain but here is my program which I wrote and the problem is the column like ID Midterm Final, it doesn't fit with the score below
like the example does. how do I solve this? and how do I turn int into float?

upload_2017-12-16_13-12-37.png
 

WBahn

Joined Mar 31, 2012
26,398
You read about "formatted output".

What do you mean by turning an int into a float? For what purpose? Give some kind of example to make it clear what you are trying to do.
 

spinnaker

Joined Oct 29, 2009
7,835
Please provide your code in text as opposed to a screen shot which is too difficult to read.

But my guess is that you need to use tabs between your columns instead of spaces.

And no idea where you are going with the int to a float either. Explain how this has something to do with your solution please.
 

xox

Joined Sep 8, 2017
608
I don't know how to explain but here is my program which I wrote and the problem is the column like ID Midterm Final, it doesn't fit with the score below
like the example does. how do I solve this? and how do I turn int into float?
Wikipedia presents a decent overview of formatted output for the printf-family of functions in this article. From my experience at least, the best way to go about it is to pass the width and precision settings as variables just to keep things as manageable and flexible as possible. Here's a simple example of creating left-justified output:

Code:
#include <stdio.h>
#include <stdlib.h>
#define COUNT 6
int
    main(void)
{
    int
        width = -12,
        precision = 2;
    const char*
        categories[COUNT] = { "ID", "Midterm", "Final", "Lab", "Score", "Grade" };
    float
        values[COUNT] = { 1, 38.8294, 67.981779, 24.4185, 91, 100.635 };
    for(size_t index = 0; index < COUNT; ++index)
        printf("%*s", width, categories[index]);
    printf("\n");
    for(size_t index = 0; index < COUNT; ++index)
        printf("%*.*f", width, precision, values[index]);
    printf("\n");
    return 0;
}
Output:

output.png

You probably don't want the ID field to be printed as a float, but again, just an example to build on...
 
Last edited:
Thread starter Similar threads Forum Replies Date
Umar19 Programming & Languages 0
Top