Adjust text to align

0

In a project I have to show the following on the screen:

La Jornada con mas puntos es la 5 con 344 puntos.
> Jornada 5
-----------------------------------------
R._Madrid 84 - 91 Barcelona
  Manresa 84 - 85 Joventut

The problem is that I have something like this:

R._Madrid 84 - 91 Barcelona
Manresa 84 - 85 Joventut

What could I do to align it according to the script? I thought about counting the number of characters in the name, but being a non-string data type, I think it will not work.

Thanks

    
asked by ElPatrón 04.04.2017 в 15:52
source

2 answers

2

You can do it in manual plan, that is, calculate the size of each text and add spaces to convenience or you can use the utilities of the standard library

option 1: library iomanip

std::cout << std::setfill(' ') << std::setw(12) << "Real Madrid"
          << ' ' << 84 << " - " << 91 << ' '
          << "Barcelona" << '\n';

Exit:

 Real Madrid 84 - 91 Barcelona
     Manresa 84 - 85 Juventud

option 2: modifiers cout

std::cout.fill(' ');
std::cout.width(12);
std::cout << "Real Madrid"
          << ' ' << 84 << " - " << 91 << ' '
          << "Barcelona" << '\n';

You could format each field to play also with the right and left alignments, but the reading and understanding of the example is complicated

    
answered by 04.04.2017 / 16:06
source
1

You can use cout but you should know that cout has some methods that help you format the output.

WIDTH

    cout <<"12345678901234567890123456789012345678901234567890" <<endl;
    // le asigno un espacio de 25 caracteres a cada equipo
    cout.width(25); cout << left << "NOMBRE DEL EQUIPO 1";
    // le asigno un espacio de 1 al guion
    cout.width(1); cout << "-";
    //le asigno su espacio y lo alineo a la derecha
    cout.width(25); cout << right << "nombre segundo equipo" <<endl;

You can review more of this in reference width

    
answered by 04.04.2017 в 16:07