Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Looping and calculating a final cost...
Author Message
Hunter007

Posted: 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**

Hunter007

Posted: 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. Thanks in advance.

Thuged_Out_G

Posted: 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

Posted: 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 ) 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

Posted: 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

Posted: 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
poly

Posted: 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

Posted: 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

Hunter007

Posted: 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

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

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

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 11 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: