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

Asked

Viewed 472 times

6

I wanted to place the columns closer so that it doesn’t 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;
}

espaçado outro exemplo

1 answer

5


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

Note that the tab in fact is flexible, it jumps every 8 characters, so if what is written has 1 character and 7 spaces, if it has 3 characters will have 5 spaces until you reach the call tab stop. With spaces you don’t have this, but you can guarantee that all the writings have 1 character is easy.

If you had a situation where the number of characters was variable then 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 the entire expected area. This can be done with standard formatting or through a padding of string manual.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.