Page 1 of 1

print using doesn't space correctly?

Posted: Sat Feb 13, 2016 6:57 pm
by student99
I'm learning qb64. I have a class problem where I have to print in columns with headings and for dollar-valued fields I have to align the columns by decimal. The chapter covers PRINT USING and I assumed that I was supposed to use that. I assumed that PRINT USING was like a formatted print in other languages; e.g., printf "%02d %12.4f", d, x; would print an integer in two columns, skip a column, and print a float using 12 columns with four decimals. The examples in the chapter suggest this interpretation for PRINT USING and I expect PRINT USING "## #######.####"; d; x to do about the same thing.

But in my program, the numbers don't seem to print formatted. Here's the code to print column headers:

Code: Select all

  C$ = "          \          \   \           \  \            \ \          \ \          \"
  Print using C$; "beginning"; "ending"; "cost of"; "average"; " "
  Print using C$; "inventory"; "inventory"; "goods sold"; "inventory"; "turnover"
and here's the code to print the numerical values:

Code: Select all

  N$ = "          #########,.##    #########,.##  $#########,.## #########,.# #########,.#"
  Print using N$; beginv; endinv; cogs; avginv; turnover
In the output (see below), the column headers seem to be aligned using C$ abut the numbers are not similarly aligned using N$. Am I misunderstanding how PRINT USING works? Is there a better way to align numerical column by decimal place?
output showing incorrect formatting
output showing incorrect formatting
Capture.JPG (26.58 KiB) Viewed 7529 times
When I look at the output, it seems like the literal spaces in N$ are being printed but the numbers are not being forced into the required fields. Is that not the correct interpretation of PRINT USING?

Also, the book uses a format like "###,###,###.##" but the qb64 wiki suggests "#########,.##" Is that the same? And is that fourteen columns? Or 13 columns? Or?

Re: print using doesn't space correctly?

Posted: Mon Feb 15, 2016 10:59 am
by burger2227
PRINT USING with text using the slashes makes it more difficult to align. Try just using PRINT and align the text with template by eye checking the screen.

Code: Select all

beginv = 10: endinv = 5: cogs = 200: avginv = 75: turnover = 5
PRINT "     beginning           ending          cost of      average     turnover"
N$ = " #########,.##    #########,.##  $$#########,.## #########,.# #########,.#"
PRINT USING N$; beginv; endinv; cogs; avginv; turnover
$$ in template places $ at start of number

Numbers fill in # from decimal point or right without one. Empty # values do take up space so only use as many digits as necessary for the entry. QB64 can make program screens larger if needed.