2

I'm trying to make sure that the second column in the output is aligned and it seemed like setw would be the solution but not matter what I do the second column is always off. This is the output I get from the code below...

1> 123
10> 234

but I want it to be...

1>   123
10>  234

The only other thing I can think of is to actually get the number of digits of what the actual number of elements are and the index then do some sort of length calc from that. That seems like a lot of handling just to get the second column right aligned.

I also tried << right but since I'm printing line by line in a loop this won't make a difference

int main()
{
    int array[2] = {123,234};
    int array2[2] = {1, 10};

    for(int i = 0; i < 2; i++){
        cout << array2[i] << "> " << setw(4) << array[i] << endl;
    }

    return 0;
 }
cpd1
  • 777
  • 11
  • 31

2 Answers2

3

Using setw ():

You need to set width for the array2[i] element instead of the array[i] element to get the alignment you are looking for.

cout << setw (2) << array2[i] << "> " << array[i] << endl;

Alternative Method 1:

Use printf for formatted printing here.

printf ("%-2d> %4d\n", array2[i], array[i]);

%-2d - the -2 left aligns the integer with width 2.

%4d - the 4 right aligns the integer with width 4.

Alternative Method 2:

Use tabs, or the \t character.

cout <<  array2[i] << ">\t" << array[i] << endl;

The \t moves your cursor bar to the next tabstop and so you end up getting data aligned in columns like you need. I would not recommend that you use this method because tab widths are unpredictable.

Madhav Datt
  • 1,065
  • 2
  • 10
  • 25
  • Thanks! For the first method, it'll push 1> one space to the right. I wanted to keep it left aligned. I'll try the printf option though! I have to add strings as well but should be doable – cpd1 Feb 07 '16 at 03:48
  • I was still having issues getting the format I wanted but that's because of the character in front of the number. I ended up converting the number to a string and concatenating it into the character. I was then able to use setw properly – cpd1 Feb 09 '16 at 03:50
2

Use a single '\t' character, it TABs automaticly the other column. Example:

for(int i = 0; i < 10; i++)
      cout << "x=" << (rand()%10000000+2000)/30 << "\t\ty=" << rand()%2000 << endl;

The output was:

x=+231096       y=+1383
x=+154630       y=+777
x=+141344       y=+1793
x=+325416       y=+1386
x=+321447       y=+649
x=+16400        y=+362
x=+84068        y=+690
x=+250530       y=+1763
x=+12847        y=+540
x=+115257       y=+1172

NOTE: I'm using g++ with flag -std=c++11 for C++11, I don't know its affects results.

  • Yes, and semantically correct, but in practice this may not have the desired effect. That's because tab-widths are unpredictable and your data may not fit some individual's setting. It's arguably better to go heretic and use spaces here. – Lightness Races in Orbit Feb 07 '16 at 03:34
  • Tab does align it but yeah I don't have control over the number of spaces. Technically what I wanted though – cpd1 Feb 07 '16 at 03:48