|
Post by bencooper on Dec 25, 2018 2:22:40 GMT -5
I am converting programs from other Basic into LB. why can't I print the variable "Amount" calculated in the Subroutine "Interest"? Seems to be a Global variable issue, as Amount does calculate inside the subroutine?
Can I use USING("###,###.##") ie with comma formatting in LB?
see my program below for compound interest calculation.
'************************************************
'** Prog5.2Compint - Compound Interest **
'** ---------------------------------- **
'** Programming in QuickBASIC, N.Kantaris **
'** 20 Dec 2018 **
'** Program to demonstrate subprogram using **
'** the Call/END SUB statements **
'** **
'** Program to be adapted to LB. **
'** PRINT USING/FORMAT not working? **
'************************************************
'DECLARE SUBInterest(Principle,Rate,N,Amount)
'REM COMPOUNT INTEREST
'DEFINT N,Y
Global Amount,Original,Rate,Years
Amount=0
PRINT "Enter original money lent: ?"
INPUT Original
PRINT "Enter interest rate (%):"
INPUT Rate
PRINT "Enter number of years :?"
INPUT Years
PRINT SPACE$(3);"YEAR";SPACE$(27);"AMOUNT"
FOR N=1 TO Years
CALL Interest Original,Rate,N,Amount
'Form$="####.##"' REM needs formatting
'Forms$="#######.##"
PRINT USING("####.##",N);SPACE$(10);Amount
'PRINT USING("#######.##",Amount)
NEXT N
END
SUB Interest Original,Rate,N,Amount
Amount=Original*(1+Rate/100)^N END SUB
|
|
|
Post by tsh73 on Dec 25, 2018 2:32:07 GMT -5
Hello bencooper I fixed your program, see for ('!!) in comments. Really, having variable global and pass it into sub (or making it return argument) doesn't look good. As for USING("###,###.##") LB does not supports it, just USING("######.##") There were some user-defined functions posted that inserted comma there needed I made mine - see it after your program. Use it if you like.
'************************************************ '** Prog5.2Compint - Compound Interest ** '** ---------------------------------- ** '** Programming in QuickBASIC, N.Kantaris ** '** 20 Dec 2018 ** '** Program to demonstrate subprogram using ** '** the Call/END SUB statements ** '** ** '** Program to be adapted to LB. ** '** PRINT USING/FORMAT not working? ** '************************************************
'DECLARE SUBInterest(Principle,Rate,N,Amount) 'REM COMPOUNT INTEREST 'DEFINT N,Y 'Global Amount,Original,Rate,Years '!! I don't see you need any globals at all
Amount=0 PRINT "Enter original money lent: ?" INPUT Original PRINT "Enter interest rate (%):" INPUT Rate PRINT "Enter number of years :?" INPUT Years PRINT SPACE$(3);"YEAR";SPACE$(27);"AMOUNT" FOR N=1 TO Years CALL Interest Original,Rate,N,Amount 'Form$="####.##"' REM needs formatting 'Forms$="#######.##" PRINT USING("####.##",N);SPACE$(10);Amount 'PRINT USING("#######.##",Amount) NEXT N END
SUB Interest Original,Rate,N, byRef Amount '!!byRef if you return it Amount=Original*(1+Rate/100)^N END SUB
User-defined USING to make thousand separator:
randomize .5
print "String";tab(15);"Val";tab(30);"Using";tab(50);"using$" for i = 1 to 20 n = int(rnd(1)*10) a$="" for j = 1 to n a$=a$;int(rnd(1)*10) next a$=a$;"." for j = 1 to 3 a$=a$;int(rnd(1)*10) next if rnd(1)<0.5 then a$="-";a$ a=val(a$) print a$;tab(15);a;tab(30);using("############.##", a);tab(50);using$("###,###,###,###.##", a) next
print print "Also can do:" x=1234567.8901 mask$ = "###,###,###.##" print x, ">";mask$;"<", ">";using$(mask$, x);"<" mask$ = "x=###,###,###.##$" print x, ">";mask$;"<", ">";using$(mask$, x);"<" mask$ = "any text ###,###,###.## more" print x, ">";mask$;"<", ">";using$(mask$, x);"<" mask$ = "### ###,###.##" print x, ">";mask$;"<", ">";using$(mask$, x);"<" mask$ = "### ### ###.##" print x, ">";mask$;"<", ">";using$(mask$, x);"<"
function using$(mask$, x) delim1000$ = "," 'the point is, we do not print them (print " ") if there are no digit in this place
realMask$ = "" for i = 1 to len(mask$) c$=mid$(mask$,i,1) if instr("#.", c$) then realMask$ = realMask$ + c$ next
x$=using(realMask$, x) 'print x$
xx$="" j = 1 digitStarted=0 for i = 1 to len(mask$) c$=mid$(mask$,i,1) if instr("#.", c$) then cc$=mid$(x$,j,1) xx$ = xx$ + cc$ j=j+1 digitStarted = (instr(" -", cc$)=0) else if instr(delim1000$, c$) then if digitStarted = 0 then if cc$="-" then 'if we have " -," we should convert it to " -" xx$ = left$(xx$, len(xx$)-1) + " " c$="-" else c$=" " 'skip thousand delimiter if digits not started yet end if end if end if xx$ = xx$ + c$ end if next using$ = xx$ end function
|
|