-4

I'm trying to create a program that will generate a character sheet that I will be able to change easily. However, I want it to look good so I'm going to be using some ASCII art to create borders within the sheet. I was wondering how I could create borders that utilized | and - but these are going to move when the numbers change.

I imagine there is some function I can use that might be able to place the ending | in the exact spot it should be and not move when the number goes from 6 to 16 or 16 to 116, or words and letters.

If anyone knows how to do this I would appreciate it highly.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Paden
  • 27
  • 6
  • 4
    Not really enough information for an answer, but you might want to meditate on why printf returns what it returns. – rici Jan 19 '16 at 05:25
  • For C++, if you prepare the lines of output in `std::string` or `std::stringstream` objects, you can inspect the current length and add appropriate padding before adding your "drawing" characters. – Tony Delroy Jan 19 '16 at 05:39
  • 2
    First step: Decide for a language. – Ulrich Eckhardt Jan 19 '16 at 05:46

1 Answers1

1

You can use field-width specifiers in printf. For left-justification, you add the - prefix. e.g.

#include <stdio.h>

int main(void)
{
    printf( "| Greed    : %-10d |\n", 6 );
    printf( "| Gluttony : %-10d |\n", 16 );
    printf( "| Lust     : %-10d |\n", 116 );
    return 0;
}

Output:

| Greed    : 6          |
| Gluttony : 16         |
| Lust     : 116        |

Alternatively, you could adopt a more programmatic strategy, as suggested by rici in the comments. That would utilize the return value (if non-negative), which tells you how many characters were written.

Read up more on printf and its format specifiers here: http://en.cppreference.com/w/cpp/io/c/fprintf

paddy
  • 60,864
  • 6
  • 61
  • 103