0

Seems like a trivial question, but I can seem to figure it out. How to make 2 outputs a certain distance appart regardless of size of first input?

For example this is my current output:

Dell       $1200
Toshiba       $2000
Sony Vaio       $1000

The spaces are uneven when I print them out. I want the output to look like the following:

Dell       $1200
Toshiba    $2000
Sony Vaio  $1000

Basically I want the distance to be the same between the 2 outputs regardless of the size of the first input.

This is my code:

printf("\nEnter a laptops name: ");
scanf("%s", &laptopName);
printf("Enter its price: ");
scanf("%d", &laptopPrice);
printf("\n");

printf("%s     ", laptopName);
printf("$%d", laptopPrice);
Benjer
  • 153
  • 2
  • 10
  • 2
    possible duplicate of [C - How to justify output lines using printf() to get equal length?](http://stackoverflow.com/questions/1809399/c-how-to-justify-output-lines-using-printf-to-get-equal-length) – jpw Mar 28 '15 at 00:05
  • Do what R Samuel Klatchko said, but if you want to be truly independent of input size, replace -20 with maximum input length. Just in case somebody writes "my super awesome no-brand low-cost computing machine". Or on the other hand...HP, ibm, dell... those would produce lost of emptiness – zubergu Mar 28 '15 at 00:21

1 Answers1

0

Specify a field width (negative value to left align):

printf("%-20s %d", laptopName, laptopPrice)
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187