Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help formatting output to file
#1
Am writing a program to keep track of a farms checkbook. Need to print out info to a file or screen that has a spread sheet look to it. Will print out date, code, discription, -w,+d,balance. That I can do, but I need all the items to line up and have a | between them. It needs to look like a spread sheet. Can anyone help me with formatting this please?
ou have to choices. I can kill you or drive you mad!

Well, at least I have choices
Reply
#2
I would suggest you to look up the keywords LEN and SPACE$.
For more detailed help, show us how far you've got (code) and
which exact parts you need help with.
/post]
Reply
#3
SUB create
INPUT "What is name of new register"; regnam$
OPEN regnam$ +".ckb FOR OUTPUT AS #1
PRINT #1, "DATE |CODE|TRANSACTION DESCRIPTION|-W |+D |BALANCE|"
CLOSE #1

press$ = INKEY$
b=0

DO
INPUT "Please enter date of transaction: ", trdate$
INPUT "Please enter check number or tranaction code: ", trcode$
INPUT "Please enter transaction description: ",trdisc$
INPUT "Was this transaction a withdrawal or deposit (w/d)"; chc$
chc$ = ucase$(LEFT$(chc$, 1))
IF chc$ = "W" THEN INPUT "Amount:", w: b = b - w
IF chc$ = "D" THEN INPUT "Amount:", d: b = b + d

OPEN regnam$ + ".ckb" FOR APPEND AS #1
PRINT #1, trdate$, "|"; trcode$, "|"; trdisc$, "|"; w, "|"; d, "|"; b, "|"
CLOSE #1
LOOP UNTIL press$ = CHR$(027)

END SUB

That's code. The first PRINT #1 is the way I want it formatted.
ou have to choices. I can kill you or drive you mad!

Well, at least I have choices
Reply
#4
Sorry the code didn't post quite right.
|8 spaces|4spaces|24 spaces|10 spaces|10 spaces|10 spaces|
is the format. spaces includes data. So date would be 05/11/06 with no leftover spaces. The program is putting in spaces.
ou have to choices. I can kill you or drive you mad!

Well, at least I have choices
Reply
#5
You're doing a good job of coding, and deserve some help.

To match your line of code:
Code:
PRINT #1, trdate$, "|"; trcode$, "|"; trdisc$, "|"; w, "|"; d, "|"; b, "|"
with your stated desired spacing:
Quote:8 spaces|4spaces|24 spaces|10 spaces|10 spaces|10 spaces|
I would use:
Code:
IF w = 0 THEN w$ = "" ELSE w$ = STR$(w)
IF d = 0 THEN d$ = "" ELSE d$ = STR$(d)
b$ = STR$(b)
PRINT #1, trdate$;SPACE$(8-LEN(trdate$));"|";trcode$;SPACE$(4-LEN(trcode$));"|";trdisc$;SPACE$(24-len(trdisc$));"|";w;SPACE$(10-len(w$));"|";d;SPACE$(10-LEN(d$));"|";b;SPACE$(10-LEN(b$));"|"
Since ther is no guarantee that dates such as 5/8/06 will actually be entered in the format 05/08/06, the SPACE$(8-len(trdate$)) will safeguard against this.[/code]
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#6
nice candidate for a function, all those repetitive space calls. check this:


Code:
Function stringPad( stringOutput As String, padLength As Integer, padCode As Integer ) As String
  
  stringPad = stringOutput + String$( padLength - Len( stringOutput ), padCode )
  
End Function


Function stringPadSpaces( stringOutput As String, padLength As Integer ) As String
  
  stringPadSpaces = stringPad( stringOutput, padLength, Asc( " " ) )
  
End Function

Sub create
  
  Dim stringOutToFile As String
  
  Input "What is name of new register"; regnam$

  Open regnam$ +".ckb" For Output As #1
    Print #1, ""
    Print #1, "        |    |                        |          |          |          |"
    Print #1, "  DATE  |CODE|TRANSACTION  DESCRIPTION|    -W    |    +D    | BALANCE  |"
    Print #1, "        |    |                        |          |          |          |"

  Close #1
  
  press$ = Inkey$
  b=0
  
  Do
  Input "Please enter date of transaction: ", trdate$
  Input "Please enter check number or tranaction code: ", trcode$
  Input "Please enter transaction description: ",trdisc$
  Input "Was this transaction a withdrawal or deposit (w/d)"; chc$

  chc$ = UCase$(Left$(chc$, 1))
  If chc$ = "W" Then Input "Amount: ", w: b = b - w
  If chc$ = "D" Then Input "Amount: ", d: b = b + d
  
  Open regnam$ + ".ckb" For Append As #1

    stringOutToFile = stringPadSpaces( trdate$, 8 ) + "|" + stringPadSpaces( trcode$, 4 ) + "|" + stringPadSpaces( trdisc$, 24 ) + "|" + stringPadSpaces( w$, 10 ) + "|" + stringPadSpaces( d$, 10 ) + "|" + stringPadSpaces( b$, 10 ) + "|"
  
    Print #1, stringOutToFile

    Print #1, ""

  Close #1

  Loop Until press$ = Chr$(027)

End Sub

all wrapped up :>
Reply
#7
Wow. It's good to see some positive help with zero criticism. That's more like it. Keep it up guys!
Screwing with your reality since 1998.
Reply
#8
Thanks for all the help. Sorry it took me so long to get back to y'all.
Life happened. I hope to be able to have a few working programs soon.
ou have to choices. I can kill you or drive you mad!

Well, at least I have choices
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)