Home > Net >  Printf print whitespace depending on space taken by other printed values
Printf print whitespace depending on space taken by other printed values

Time:12-29

I'm doing a small RPG-textbased game in c, as an introductory exercise for C.

So, I'm doing some prints and also printing some integers inside those prints.

Currently, my very simple example looks like this (this is just with hardcoded values for sake of the example):

int myHealth = 200;
int monsterhealth = 200;
printf("       | You    |  Monster  | \n");
printf("Health | %d    | %d       | \n", myHealth, monsterhealth);

Which prints to this:

       | You    |  Monster  | 
Health | 200    | 200       | 

But if the number instead only has one digit, like 1, then it has the simple issue that the text becomes a little bit offset, like this:

       | You    |  Monster  | 
Health | 1    | 1       | 

This seems like a very simple issue, and I guess I could solve it by not having tables in this way, but now I got a little stubborn.

What is the cleanest way of adding some padding to a string when the integer doesnt take up as much space?

CodePudding user response:

You can use a field size to specify how much space a number (or string) should take -- whitespace will be added to make it (at least) that size. A positive number will add space on the left (right justified), while a negative number will add it on the right (left justified). So you can do something like:

#define NUM_STATS 2
struct Character {
    char *name;
    int  stats[NUM_STATS];
};

const char *stat_names[NUM_STATS] = { "Health", "AC" };

// character[0] is the player, others are monsters
struct Character characters[MAX_CHARACTERS];
int num_characters;

void print_info() {
    printf("%-8s |", "");
    for (int i = 0; i < num_characters;   i)
        printf("%-4s |", character[i].name);
    printf("\n");
    for (int s = 0; s < NUM_STATS;   s) {
        printf("%-8s |", stat_names[s]);
        for (int i = 0; i < num_characters;   i) {
            int len = strlen(character[i].name);
            if (len < 4) len = 4;
            printf("%*d |", -len, character[i].stats[s]); }
        printf("\n");
    }
}

If you have too many characters, this will wrap (and look ugly), but you can deal with that by keeping track of how wide your printing is (and how wide your screen is) and splitting up the table in that case.

CodePudding user response:

What is the cleanest way of adding some padding

Determine minimum width for each column and use a minimum field width via "*".

Use "-" flag to left justify.

  int myHealth = 200;
  int monsterhealth = 200;

  int cAt = 7;
  int cYou = 7;
  int cMon = 7;

  printf("%*s | %-*s | %*s |\n", //
      cAt, "", cYou, "You", cMon, "Monster");
  printf("%*s | %-*d | %*d |\n", //
      cAt, "Health", cYou, myHealth, cMon, monsterhealth);
  printf("%*s | %-*d | %*d |\n", //
      cAt, "One", cYou, 1, cMon, 1);

Sample Output

        | You     | Monster |
 Health | 200     |     200 |
    One | 1       |       1 |
  •  Tags:  
  • c
  • Related