Using c++ setw to try to align second column -
i'm trying make sure second column in output aligned , seemed setw solution not matter second column off. output code below...
1> 123 10> 234 but want be...
1> 123 10> 234 the other thing can think of number of digits of actual number of elements , index sort of length calc that. seems lot of handling second column right aligned.
i tried << right since i'm printing line line in loop won't make difference
int main() { int array[2] = {123,234}; int array2[2] = {1, 10}; for(int = 0; < 2; i++){ cout << array2[i] << "> " << setw(4) << array[i] << endl; } return 0; }
using setw ():
you need set width array2[i] element instead of array[i] element alignment looking for.
cout << setw (2) << array2[i] << "> " << array[i] << endl; alternative method 1:
use printf formatted printing here.
printf ("%-2d> %4d\n", array2[i], array[i]); %-2d - -2 left aligns integer width 2.
%4d - 4 right aligns integer width 4.
alternative method 2:
use tabs, or \t character.
cout << array2[i] << ">\t" << array[i] << endl; the \t moves cursor bar next tabstop , end getting data aligned in columns need. not recommend use method because tab widths unpredictable.
Comments
Post a Comment