1

I want output as per follows in C++:

name                   aabsd
Size in KB             170 
Width and Height       512 512

cout<<"\n \t name "<<std::setw(15)<<filename;
cout<<" \n \t Size in KB "<<std::setw(10)<<size;
cout<< " \n \t Width  and Height  "<<std::setw(3)<<width<<" "<<height;

Values on right side should be aligned in same coloumn. I tried with setw() but it does not give me output aligned because my left side text is different.

debonair
  • 2,505
  • 4
  • 33
  • 73
  • please show your attempt – Pavel Jun 03 '14 at 17:04
  • 1
    See related question http://stackoverflow.com/questions/2485963/c-alignment-when-printing-cout. – Mihai8 Jun 03 '14 at 17:04
  • Also use `setw()` for the left side parts (without the `\n`!), to get everything aligned. Don't use `\t`, it depends on terminal settings and if the length of a part hits the tab length, the next tab position will be chosen (not really relevant if you have them at the beginning of the output lines). – πάντα ῥεῖ Jun 03 '14 at 17:40

3 Answers3

2

Another answer that relies on purely C++ specific constructs.

#include <iostream>
#include <iomanip>

int main()
{
   char const* filename = "abcd";
   int size = 10;
   double width = 20;
   double height = 30;

   // std::left says align the output to the left when writing the next field
   // set::setw(20) says use 20 characters for the next field.
   std::cout << std::left << std::setw(20) << "name" << filename << std::endl;
   std::cout << std::left << std::setw(20) << "Size in KB" << size << std::endl;
   std::cout << std::left << std::setw(20) << "Width and Height" << width << " " << height << std::endl;

   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
-2
printf( "%-20s%-20s", "name", "aabsd" );
printf( "%-20s%-20d", "Size in KB", 170 );
printf( "%-20s%-20d %d", "Width and Height", 512, 512 );

-20%s will left justify 20 spaces

20%s will right justify 20 spaces

Kris Morness
  • 574
  • 5
  • 12
-2

Try using \t to align the output to the next tab position.

printf("Name\t%s\n",name);
printf("Size in KB\t%d\n",size);
printf("Width and Height\t%d%d\n",width,height);

That will make the second column aligned, maybe you will need a second tab in one of them, but thats it.

Another way is to manually move the cursor to where you want it. This should do

SetCursorPos(xpos,ypos);

You need to include windows.h

  • 1
    Same problem as with the other answer! And proposing TAB makes it even worse! – πάντα ῥεῖ Jun 03 '14 at 17:18
  • Try with SetCursorPos(x,y), i added it to the answer. And tab works, dont be silly :P – Whileandt Jun 03 '14 at 17:23
  • _'Try with SetCursorPos(x,y)'_ Certainly not, I'd do it with `setw()` as proposed in the question given as dupe. Also `SetCursorPos()` isn't standard. – πάντα ῥεῖ Jun 03 '14 at 17:27
  • Tab is garbage and does't work. The size of a tab is completely arbitrary and will show up differently in different contexts. Space padding is the only reliable way to do this. – tadman Jun 03 '14 at 20:30