I created an accountant program that basically lets the user add rows and does some math. My problem is, I need to make it so it prints the table on paper once the user presses a button. How can I accomplish that? Please explain it step-by-step as I’m a beginner.
EDIT: Why was this question voted down. What is wrong with it?
The basic output tool in C++ is
std::ostream, but it is very limited.It’s possible (but not always easy) to format tables using it if the
output is using a fixed width font, but this is rarely the case today.
If you can get away with using a fixed width font, the manipulators of
iostream should be sufficient; decide the width of each column, and set
the width (and alignment—left or right) using the appropriate
manipulators when you output the field.
Otherwise, you’ll have to determine what markup language the printed
output should use—Postscript is widespread, but far from univeral.
Having done that, you’ll have to iterate over lines, and in each line,
over the columns, generating the correct markup for each one. If you’re
generating something like Postscript (or most printer markup languages),
you’ll have to keep track of absolute positions, and maybe calculate
column width and such, determining the width of each field depending on
the font being used and the width of each character in that font.
More than one program I’ve seen has output a LaTeX source, and then used
systemto invoke LaTeX (orpdflatex, to generate PDF); this supposesthat LaTeX is installed on all of the machines one which the program
will run, but LaTeX will take care of all of the above calculations; you
just output your columns, separated by a
'&', with each lineterminated by two
'\', with the appropriate surrounding commands, andLaTeX does the rest. (This is the solution I’d recommend, if you can
possibly impose the presence of LaTeX. As old and as un-user-friendly
as it is, LaTeX still generates the best output of any program I’ve
tried.)