How to reduce the spacing between characters printed with \ t in C ++?

5

I wanted to put the columns closer so that they do not happen as in figure 2.

int i,j,elemento;

for(i=0; i<elemento; i++){
    for(j=-elemento; j<=elemento; j++){
        if(i >= abs(j)) {
            cout<<"\t"<<abs(j)+1;

        }
        else{
            cout<<"\t";
        }
    }
    cout<<endl;
}

    
asked by anonymous 31.08.2018 / 20:11

1 answer

5

Not using \t . Use spaces and control as many as you want, 1 or 2 seems to be adequate. The tabulation depends on the console engine you are using, but the normal one is, or was, 8.

Note that the tab is in fact flexible, it jumps every 8 characters, so if what is written has 1 character and 7 spaces, if it has 3 characters it will have 5 spaces until it reaches the tab stop . With spaces you do not have this, but it can be guaranteed that all the writings have 1 character is easy.

If you had a situation where the number of characters was variable there, you would need to format the text to always have the same amount, even if you had to put spaces to the right of it to fill all the predicted area. This can be done with standard formatting or through a padding of a string manual.     

31.08.2018 / 20:34