shopping cart calculator

General FreeBASIC programming questions.
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

shopping cart calculator

Hi all.

For my next update to my shopping cart calculator, I would like to be able to save the resulting shopping list to a text file. What is the best method for this, in your opinion?

Code: Select all

'Shopping Cart Calcuator v1.3
'A Project for Algebra 1 Class
'By Michael (Blahboybang/Fuzzypig) Reiley

Dim item_name (1 to 1000) as string
Dim item_cost (1 to 1000) as double
Dim sub_total as double
Dim total as double
Dim sales_tax as double
Dim tax_total as double

'Intro
Print "Shopping Cart Calculator v1.3"
sleep 1500
cls
sleep 500
Print "By Michael Reiley"
sleep 1500
cls
sleep 500

'Main
item_num = 1

Input "Sales Tax Percentage in Area : ",sales_tax
sales_tax = sales_tax / 100
cls
mainloop:
Input "Item Name : ",item_name(item_num)
cls
Input "Item Cost : \$",item_cost(item_num)
cls

for i = 1 to item_num
if i = 1 then tax_total = item_cost(1) * sales_tax
if i > 1 then tax_total = tax_total + (item_cost(i) * sales_tax)
if i = 1 then sub_total = item_cost(1)
if i > 1 then sub_total = sub_total + item_cost(i)
total = sub_total + tax_total
Print item_name(i) & " :     \$" & item_cost(i)
next i

item_num = item_num + 1

Print "   Sub-Total : \$"
c = csrlin
locate c-1, 16
Print using "#############.##"; sub_total

Print "       Total : \$" & total
c = csrlin
locate c-1, 16
Print using "#############.##"; total

inputhere:
Input "Enter Another Item (y/n)";yn\$
cls
if lcase(yn\$) = "y" or lcase(yn\$) = "yes" then goto mainloop
if lcase(yn\$) = "n" or lcase(yn\$) = "no" then goto ending
Print "INPUT ERROR"
sleep 1000
cls
goto inputhere

'Ending
ending:
Print "Press Any Key to Exit"
sleep
Thrawn89
Posts: 477
Joined: Oct 08, 2005 13:12
Umm...well, the only way I know of is open the file:

Wiki Documentation: OPEN

Then you Print it to the file:

Wiki Documentation: PRINT

Then you Close the file at the end:

Wiki Documentation: CLOSE

Play around with those, if you are still having trouble, post your new source and Im sure someone would be willing to help, but please make an effort first, cause this really isnt that difficult, I gave you all the tools, now you just gotta read lol, thanks ;-)

Thrawn
jevans4949
Posts: 1156
Joined: May 08, 2006 21:58
Location: Crewe, England
You might be better off using WRITE rather than PRINT, as (according to the documentation) its output is more compatible with INPUT.

Your Subtotal and following lines should have some sort of identifier so you know when you read it back.

In your subtotal / tax-calculation loop, if you zeroise the total field before starting then you don't need separate code for i=1.

Is this the correct way to calculate sales tax, or should it be x% of the total bill? Results may differ.

Be aware that adding up a long list of floating-point numbers can give a different result from what you expect. (Decimal fractions are not accurately represented in binary.) This would show up if you printed the per-item sales tax to 2 decimal places. In this sort of situation, working in cents held as integers is more reliable.
Thrawn89
Posts: 477
Joined: Oct 08, 2005 13:12
Agreed on the round-off error bit, that would be the way to do it

Hmm...a while ago I remember somebody telling me to advoid WRITE and use PRINT when you can, so Ive never really used it...oh well, use either I guess

I disagree on the read back to the INPUT....it may not be as compatable, I dont know about that, but it always worked fine for me when I used the PRINT/INPUT combo, well at least for Strings and Numbers it works ok, provide some source to prove me wrong if you wish ;-) I'll appreciate it

Thrawn
rdc
Posts: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:
If I were doing this I would probably use a type-def array for my items. You can then save the type def to disk, which is a lot easier than trying to save a whole bunch of diffeent variables. I use this strategy in DDD:

Code: Select all

type maptype
map(dunw, dunh) As Integer     'map array
traps(dunw, dunh) as traptype
item(dunw, dunh) as invtype    'item array
isseen(dunw, dunh) as integer  'has player seen location
visited(dunw, dunh) As Integer  'has player seen location
isdoor(dunw, dunh) As doortype 'list of doors on map
issecret(dunw, dunh) As Integer 'secret flags
soundmap(dunw, dunh) As Double
monster(1 To monarray) As montype 'monsters on map
isdark as integer              'is level dark
startx as integer              'stair up location
starty as integer
endx as integer                'stair down location
endy as integer
lcount(1 To maxlevels) As Integer 'level counter
tmaptype As Integer               'map type cavern or dungeon
currlevel As Integer              'current level
mapscheme As Integer              'graphic map scheme
showhealth As Integer
end type

Dim Shared levels As maptype 'level info

Code: Select all

Sub SaveGame
Dim fh As Integer

fh = FreeFile
Open "dddlvl.dat" For Binary As #fh
Put #fh,,levels
Close #fh
fh = FreeFile
Open "dddply.dat" For Binary As #fh
Put #fh,,player
Close #fh
PrintMessage "Game saved."
End Sub
jevans4949
Posts: 1156
Joined: May 08, 2006 21:58
Location: Crewe, England
rdc wrote:If I were doing this I would probably use a type-def array for my items. You can then save the type def to disk, which is a lot easier than trying to save a whole bunch of diffeent variables.

That is probably a good approach if you are dumping a save file for a game, but probably less desirable if you are handing off a file to another program.

Looking at that code again, have you heard yet that goto is considered harmful? May I suggest the following? I have left your labels in for identification, although they in fact become unnecessary.

Code: Select all

ans% = 1
mainloop:
while ans% = 1
Input "Item Name : ",item_name(item_num)
'
' whatever
'
inputhere:
Ans%=0
while ans% = 0
Input "Enter Another Item (y/n)"; yn\$
Select case lcase(yn\$)
case "y","yes"
ans% = 1
case "n","no"
ans% = 2
case else
print "INPUT ERROR"
end select
wend
wend 'mainloop while
ending:
' whatever
end

I would also take out most of your cls statements - better in this case for the user to have previous input still on screen, so he knows how far he as got.

Looking yet again, why are you adding up the whole bill from scratch every time you get a new item? Clear the totals at the top of the bill, then just add in the new item as it is keyed.
Thrawn89
Posts: 477
Joined: Oct 08, 2005 13:12
Ooh, wow, didnt even see the GOTOs...yeah, I second his post 100% ^

Thrawn