0

I have some loop:

for(int i=0; i<rolls; i++)
    {
        first = rand()%6+1;
        second = rand()%6+1;
        cout<<'('<<first<<')';
        for(int j=0; j<first; j++)
        {
            cout<<'*';
        }
        cout<<'\t'<<'('<<second<<')';
                for(int j=0; j<second; j++)
        {
            cout<<'*';
        }
        cout<<endl;
    }

It runs great but i have some problems with my output:

CPU             Player
(2)**   (3)***
(4)**** (1)*
(6)******       (5)*****
(5)*****        (6)******
(1)*    (5)*****
(3)***  (1)*
(4)**** (2)**
(3)***  (1)*
(1)*    (5)*****
(5)*****        (4)****
(2)**   (6)******
(5)*****        (3)***

Whats wrong with this tabulations?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Atre
  • 7
  • 4
  • 1
    So what's the output supposed to look like? I know you want a table but your description of the problem needs to be more precise than that. Why not provide a sample of what the expected output looks like? – In silico Jun 27 '11 at 21:52
  • 2
    Nothing. They work just as documented. – Kerrek SB Jun 27 '11 at 21:52
  • Please read the fine manual. This is exactly what you should expect from a tabstop. – jpm Jun 27 '11 at 21:55
  • possible duplicate of [C++ alignment when printing cout <<](http://stackoverflow.com/questions/2485963/c-alignment-when-printing-cout) – littleadv Jun 27 '11 at 21:57
  • Oh, so fast. I like this place. Thanks everyone, i have solved my problem. – Atre Jun 27 '11 at 22:03

5 Answers5

2

\t relies on tab stops that are dependent on your environment.

Instead, you should look at the iomanip header for specifying minimum column spans. See also this answer.

Community
  • 1
  • 1
retrodrone
  • 5,850
  • 9
  • 39
  • 65
2

You're asking why it is not aligned in two neat columns?

If so that's because \t moves the caret to the next tab stop from where you are. If you're at the tab stop already - you'll move one more than what you might have expected. When you print 6 chars and then \t - you'll jump to position 8. If you print 8 chars and then \t you'll jump to position 16 (assuming tabs are every 8 positions).

That's what happens in your case.

edit

That's the answer to your question What's wrong. How to solve it - see @retrodrone's answer or the duplicate post.

Community
  • 1
  • 1
littleadv
  • 20,100
  • 2
  • 36
  • 50
0

The width of \t is highly dependent on your specific environment. In your case, it looks like you have a tab width of 8; that is, \t adds up to eight spaces, until the current column is divisible by 8. Since (5)***** is eight characters long, \t must move to the next tab position, and adds another 8 spaces. But (4)**** is seven characters long, so \t can get away with one space.

In short: Don't use \t, explicitly move over however many spaces you want.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
0

Change

cout<<'\t'<<'('<<second<<')';

to

cout<< setw(16) << '(' << second << ')';

and #include <iomanip>

Printing a \t character is just liking pressing the tab key on your keyboard: it moves it to the next tab stop, not a particular tab stop.

Using std::setw you can specify a particular width.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
0

Nothing's wrong.

Tabspaces do not automatically tabulate your content for you. They simply advance the caret to the next available tabstop.

"Next available tabstop" means the next tabstop at which there is not already a character. In the case of your console, your tabstops are set such that the first tabstop happens to fall within the expected length of your first "column" of data.

Since tabstops are always, by design, configurable by the end-user's display mechanism, they are not really appropriate for tabulating.

Use fixed whitespace instead, so that you can guarantee the layout.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055