print using doesn't space correctly?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
student99
Newbie
Posts: 1
Joined: Sat Feb 13, 2016 6:28 pm

print using doesn't space correctly?

Post 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 7136 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?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: print using doesn't space correctly?

Post 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.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply