Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Looping and calculating a final cost...
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Hunter007




PostPosted: Sun Jan 11, 2004 1:35 pm   Post subject: Looping and calculating a final cost...

Hey, I need some help on a program I am currently working on. It is a cash register program. I have it looped so that the user can enter in as many items as they want. It can be ended by typing in "done" as the product name. The program works fine, but instead of ending I want it to calculate a subtotal, PST, GST and final cost for all of the items together.

**The program calculates subtotal, pst, gst, and final cost for each item**
Sponsor
Sponsor
Sponsor
sponsor
Hunter007




PostPosted: Sun Jan 11, 2004 3:01 pm   Post subject: (No subject)

Does anyone have an idea that might help me out? I need to finish this by the end of today. Crying or Very sad Thanks in advance.

*If there is already a thread for this I'm sorry I posted this topic and please give me a link to it.*
Thuged_Out_G




PostPosted: Sun Jan 11, 2004 3:02 pm   Post subject: (No subject)

store each price into a real array...then add up each element of the array and store it in a variable called sub.

you then have the sub total
PST is what 7% in ontario, and GST is 8%

so make a function for it

code:

%calculate taxes
function tax(sub:real):real
var PST,GST:real
PST:=sub*0.7
GST:=sub*0.8
result "The subtotal is ", sub," the GST is ", GST," and the PST is ", PST
end tax

once youve calculate the sub total by adding up all the elements in the array, then you get the taxes like this
code:

put tax(sub)
Hunter007




PostPosted: Sun Jan 11, 2004 3:13 pm   Post subject: (No subject)

OK thanks for the reply. Maybe I wasn't clear the first time. (Sorry about that Very Happy) I already figured out how to add the tax in and make it display the subtotal,PST,GST etc. What I need is, for when the the user types in "done as the Product, the program stops looping and adds up everything. So for each product it shows subtotal,pst,gst and final total. When user types in "Done" it shows a subtotal for all the items, pst for all the items, gst for all the items and final total for all the items.

Here's my code....See if you can make any sense of what I just asked.
code:


const GSTper : real := 0.07
const PSTper : real := 0.08

var PST : real
var GST : real
var item : string
var number : int
var cost : real
var total : real

loop

    put " * ***************************************** *"
    put " * When finished type in 'done' as product.  *"
    put " * ***************************************** *"
    put ""

    put "Product: " ..
    get item : *
    put ""

    exit when item = "done" or item = "Done" or item = "DONE"

    put "Number of items: " ..
    get number
    put ""

    put "Price of item: $" ..
    get cost

    cost := cost * number

    PST := cost * PSTper

    GST := cost * GSTper

    total := cost + GST + PST

    cls

    put "Subtotal     : $ ", cost : 6 : 2
    put "PST          : $ ", PST : 6 : 2
    put "GST          : $ ", GST : 6 : 2
    put "Final Total  : $ ", total : 6 : 2

    delay (2500)
    cls

end loop
Hunter007




PostPosted: Sun Jan 11, 2004 3:53 pm   Post subject: (No subject)

I don't mean to rush anybody, because I know how forums work. But I really need to get this done tonight, as I still have more programs to write. Please help me fix this problem.
SeeknDestroy




PostPosted: Sun Jan 11, 2004 4:25 pm   Post subject: (No subject)

ok, well to exit the loop, u should prolly have an if statement, so like enter items, then if variable is done then add the taxes and stuff, then exit the loop, sry if this doesn't help u, but im really new to turing, and i suck with it Embarassed
poly




PostPosted: Sun Jan 11, 2004 4:30 pm   Post subject: (No subject)

SeeknDestroy's way could work, Im just gonna edit the very end of your code so it looks like this:

code:

    delay (2500)
    cls
exit when item="done"
end loop


so the exit when item="done" i added will exit the loop when the user inputs the word done as an item.
McKenzie




PostPosted: Sun Jan 11, 2004 4:56 pm   Post subject: (No subject)

if I understand you, you are looking to keep a grand total. Like

code:
6 burgers @ $3.00
sub:  18.00
PST:  1.44
GST: 1.26
Total:20.70
4 drinks  @ $1.00
sub:  4.00
PST:  0.32
GST: 0.28
Total: 4.60
Grand Total: 25.30

OK, so you need an new accumulator (like your total) that adds up all of your totals. To get it to show just put your put below the end loop
Sponsor
Sponsor
Sponsor
sponsor
Hunter007




PostPosted: Sun Jan 11, 2004 5:08 pm   Post subject: (No subject)

Thank you all for your replies. I added what SeeknDestroy said and what poly said. McKenzie's reply however was exactly what I was looking for. I'm just not sure how to do it. Please help explain this.
poly




PostPosted: Sun Jan 11, 2004 5:12 pm   Post subject: (No subject)

oh gee this was messy, so i umm deleted it
McKenzie




PostPosted: Sun Jan 11, 2004 5:16 pm   Post subject: (No subject)

An Accumulator
~~~~~~~~~~
An variable used to accumulate a total.
To use one:
1. Declare your variable and initialize it to zero(usually zero)
code:
var grandTotal :real := 0

2. Increase its value buy the desired amount for each desired amount
code:
% after caculating the total
grandTotal := grandTotal + total

3. Examine the total when you are done
code:
% outside the loop
    put "Grand Total  : $ ", grandTotal : 6 : 2
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: