Prevent letters from being run when printing data on screen

2

For example, in the image depending on the first name (Chuck Norris) he runs me all the way to the side. I'm looking for a way that, independent of the length of the text, the things on the right do not run.

printf("*********************************************************************************\n");
printf("            TABLA DE ESTADISTICAS.      \n  %s:                  %s        Nivel Mapa    POSICION PJ\n",player1.nombre,enemigo1.nombre);
printf("       Vida:%d                Vida:%d           %d           Y: %d   \n       Ataque:%d               Ataque:%d                     X: %d ",player1.vida,enemigo1.vida,player1.nivel_mapa,player1.y,player1.ataque,enemigo1.ataque,player1.x);
printf("                                         \n       Drenar agua: %d         Escape:%c%d  \n ",player1.drenaragua,'%',enemigo1.escape);
printf("********************************************************************************\n");
    
asked by JGUser 22.12.2018 в 20:42
source

1 answer

5

Just as you have it, not only the name of the player "will run" the columns, also depending on how many figures have the life, attack, etc. everything will move.

You have to use the width specifiers of printf() . For example, for life to always be shown using two digits you can put:

"Vida:%02d"

Thus, if it is 8, 08 will be displayed. If you do not want the initial zero, you can put %2d , in which case instead of zero it will use a space. If you want the fill spaces instead of putting them in front of the target number, you can put %-2d .

For strings you can also specify a minimum width with %10s for example (10 letters, filling in with spaces from the left), or with %-10s (10 letters filling in with spaces from the right).

However, you can not specify a maximum width. If the name measures more than 10 letters, it will occupy what you need and therefore it will "push" everything. You must ensure by other means that the name has a maximum size and use that size in %s . (false, see edition)

Example

#include <stdio.h>

int main(void) {
  int n = 25;
  int m = 3;
  printf("|%10d|\n", n);
  printf("|%10d|\n", m);
  printf("|%10s|\n", "hola");
  printf("|%-10d|\n", n);
  printf("|%-10d|\n", m);
  printf("|%-10s|\n", "hola");
  printf("|%-10s|\n", "cadena con mas de diez letras");
  return 0;
}

Produce:

|        25|
|         3|
|      hola|
|25        |
|3         |
|hola      |
|cadena con mas de diez letras|

It's up to you to do size tests and adjust the printf() chain, removing the spaces that you had manually inserted and that you should reduce to take into account what printf() will get you due to the minimum width specifiers.

Edit

Investigating a little more about printf() I found an option that I did not know yet, and see that I've been using C. You can specify the maximum size for a string with %.10s for example (10 characters maximum). If the chain is longer, it is truncated. If it is smaller it is not modified.

It is also valid, and it would be what you are interested in in this case, a specification like %-10.10s that specifies at the same time a minimum and maximum size. If the string has less than 10 letters, it is filled with spaces on the right until it occupies 10. If it has more, it is cut when it reaches character 10 and the rest is not shown. Example:

#include <stdio.h>

int main(void) {
  printf("|%-10.10s|\n", "hola");
  printf("|%-10.10s|\n", "cadena con mas de diez letras");
  return 0;
}

Result:

|hola      |
|cadena con|
    
answered by 22.12.2018 / 21:00
source