Grocery Problem
Author |
Message |
Jurica
|
Posted: Tue Nov 04, 2008 7:02 pm Post subject: Grocery Problem |
|
|
I have a problem with this question I have to do. I've tried everything I know, I am a beginner so please don't make fun of me like "Oh your so stupid".
Question: Write a program that accepts the name of an item and the price. Have the program
calculate a sales price that includes a 10% discount. Print a sales receipt on the screen
with the name of the item, the sales price, the GST and PST and the total price. (Be sure
to round-off to 2 decimal places)
var total : real := 0
var product : int
loop
put "Welcome to my Grocery Store!"
put "Enter product (1=Corn, 2=Potatoes, 3=Artichokes, 4=Quit): "..
get product
if product = 1 then
total := total + 2.25 - 0.18
elsif product = 2 then
total := total + 6.50 - 0.18
elsif product = 3 then
total := total + 1.75 - 0.18
elsif product = 4 then
exit
end if
put "The running total is $", total : 0:2
end loop
put "10% Discount: $", total * 0.10 : 0:2
put "GST: $", total * 0.08 : 0:2
put "PST: $", total * 0.06 : 0:2
put "--------------------------"
put "Final Total: $", total * 1.14 : 0:2 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jbking
|
Posted: Tue Nov 04, 2008 7:22 pm Post subject: RE:Grocery Problem |
|
|
A few questions if you don't mind:
1) Why not let the user enter a string for the name of the product? This would be a bit easier on the user I think.
2) Why not let the user enter a quantity along with the price, e.g. they can buy 3 widgets @ $3.30 or whatever.
3) What is with subtracting .18 so often? I'm just wondering on this as it doesn't quite make sense to me.
4) Why not change the total on some of the lines near the end so that the discount is taken into account as the way it is written now, it doesn't really do that. Also, you aren't compounding the discount and taxes I think. For example, if you take a product that costs $100 and give a 10% discount and then add a 10% tax on it, what is the end result? Answer: $99 which isn't necessarily what some would know. |
|
|
|
|
|
Geniis
|
Posted: Tue Nov 04, 2008 9:28 pm Post subject: Re: Grocery Problem |
|
|
Dont worry no one here will laugh at you.. everyone is a beginner at some point right?(im still a beginer myself ive only know turing for like 2 months lol)
Anyway with your problem i think you should do sumthing like this:
Turing: |
%These are the variables in the program that will change as we go
var total : real := 0 %What the total price without the tax or discount will be stored in
var totalWithDiscount : real %What the total will be after the discount
var totalWithTax : real %Then what it will be after taxes
var product : int %What the customer wants to buy
var productList : string := "" %What we will list our products in
var amount : int %How much of a product we want to buy
%These are the constants, variables that will always remain the same
const cornPrice : real := 2. 25 %The price of corn
const potatoPrice : real := 6. 50 %The price of potatoes
const artichokePrice : real := 1. 75 %The price of artichokes
const gst : real := 0. 08 %Our gst
const pst : real := 0. 06 %Our pst
%Actual program
put "Welcome to my Grocery Store!" %Initial welcome into the store...no need to repeat it
loop
put "Enter product (1=Corn, 2=Potatoes, 3=Artichokes, 4=Quit): " .. %Prompt for product
get product %Get product
if product = 1 then %if product 1
put "How many of them would you like?" %Prompt for number of product
get amount %Get amount
total := total + (2. 25 * amount ) % This is your total so far plus the price of item selected x number of item selected
productList := productList + "Corn x " + intstr (amount ) + " \n" %This says to add Corn x amount of corn selected(we use intstr(amount) to turn amount into a string)
%And put \n to tell turing to jump to the next line(kinda like pressing enter when your typing)
elsif product = 2 then %This
put "How many of them would you like?"
get amount
total := total + (6. 50 * amount )
productList := productList + "Potatoes x " + intstr (amount ) + " \n"
elsif product = 3 then
put "How many of them would you like?"
get amount
total := total + (1. 75 * amount )
productList := productList + "Artichokes x " + intstr (amount ) + " \n" %to this Is all same as it was up there /\
elsif product = 4 then % Exit if 4 is pressed
exit
end if
put "The running total is $", total : 0 : 2 %This puts running total after every item adde to list
end loop
totalWithDiscount := total - (total * 0. 10) %Now that customer is done buying add up the total after the discount
totalWithTax := totalWithDiscount + (totalWithDiscount * gst ) + (totalWithDiscount * pst ) %And then with taxes
put "--------------------------"
put "Bought : " %Now display what was bought
put productList %---------------------------
put "Total : $", total : 0 : 2 %What there total is before taxes and discount
put "10% Discount: $", total * 0. 10 : 0 : 2 %How much is the discount
put "GST: $", total * gst : 0 : 2 %The gst total
put "PST: $", total * pst : 0 : 2 %The pst total
put "--------------------------"
put "Final Total: $", totalWithDiscount : 0 : 2 %What the final total is after taxes and discount
|
Not to complicating. Try keeping most of your numbers and stuff inside of variables and constants, makes it easier to read your program (as long as the variable name isnt completely random lol)
That should solve your probleme |
|
|
|
|
|
Jurica
|
Posted: Wed Nov 05, 2008 4:47 pm Post subject: Re: Grocery Problem |
|
|
Geniis @ Tue Nov 04, 2008 9:28 pm wrote: Dont worry no one here will laugh at you.. everyone is a beginner at some point right?(im still a beginer myself ive only know turing for like 2 months lol)
Anyway with your problem i think you should do sumthing like this:
Turing: |
%These are the variables in the program that will change as we go
var total : real := 0 %What the total price without the tax or discount will be stored in
var totalWithDiscount : real %What the total will be after the discount
var totalWithTax : real %Then what it will be after taxes
var product : int %What the customer wants to buy
var productList : string := "" %What we will list our products in
var amount : int %How much of a product we want to buy
%These are the constants, variables that will always remain the same
const cornPrice : real := 2. 25 %The price of corn
const potatoPrice : real := 6. 50 %The price of potatoes
const artichokePrice : real := 1. 75 %The price of artichokes
const gst : real := 0. 08 %Our gst
const pst : real := 0. 06 %Our pst
%Actual program
put "Welcome to my Grocery Store!" %Initial welcome into the store...no need to repeat it
loop
put "Enter product (1=Corn, 2=Potatoes, 3=Artichokes, 4=Quit): " .. %Prompt for product
get product %Get product
if product = 1 then %if product 1
put "How many of them would you like?" %Prompt for number of product
get amount %Get amount
total := total + (2. 25 * amount ) % This is your total so far plus the price of item selected x number of item selected
productList := productList + "Corn x " + intstr (amount ) + " \n" %This says to add Corn x amount of corn selected(we use intstr(amount) to turn amount into a string)
%And put \n to tell turing to jump to the next line(kinda like pressing enter when your typing)
elsif product = 2 then %This
put "How many of them would you like?"
get amount
total := total + (6. 50 * amount )
productList := productList + "Potatoes x " + intstr (amount ) + " \n"
elsif product = 3 then
put "How many of them would you like?"
get amount
total := total + (1. 75 * amount )
productList := productList + "Artichokes x " + intstr (amount ) + " \n" %to this Is all same as it was up there /\
elsif product = 4 then % Exit if 4 is pressed
exit
end if
put "The running total is $", total : 0 : 2 %This puts running total after every item adde to list
end loop
totalWithDiscount := total - (total * 0. 10) %Now that customer is done buying add up the total after the discount
totalWithTax := totalWithDiscount + (totalWithDiscount * gst ) + (totalWithDiscount * pst ) %And then with taxes
put "--------------------------"
put "Bought : " %Now display what was bought
put productList %---------------------------
put "Total : $", total : 0 : 2 %What there total is before taxes and discount
put "10% Discount: $", total * 0. 10 : 0 : 2 %How much is the discount
put "GST: $", total * gst : 0 : 2 %The gst total
put "PST: $", total * pst : 0 : 2 %The pst total
put "--------------------------"
put "Final Total: $", totalWithDiscount : 0 : 2 %What the final total is after taxes and discount
|
Not to complicating. Try keeping most of your numbers and stuff inside of variables and constants, makes it easier to read your program (as long as the variable name isnt completely random lol)
That should solve your probleme
Thank you, this was fixed my problem. |
|
|
|
|
|
syntax_error
|
Posted: Wed Nov 05, 2008 9:38 pm Post subject: Re: Grocery Problem |
|
|
Jurica @ Wed Nov 05, 2008 4:47 pm wrote:
Thank you, this was fixed my problem.
How? Jurica simply gave away the answer, I do not mean to sound like a prick; however, try reading over the forum rules? Posting full solutions does not help the person. Furthermore, its frowned upon. |
|
|
|
|
|
Jurica
|
Posted: Thu Nov 06, 2008 1:21 pm Post subject: Re: Grocery Problem |
|
|
syntax_error @ Wed Nov 05, 2008 9:38 pm wrote: Jurica @ Wed Nov 05, 2008 4:47 pm wrote:
Thank you, this was fixed my problem.
How? Jurica simply gave away the answer, I do not mean to sound like a prick; however, try reading over the forum rules? Posting full solutions does not help the person. Furthermore, its frowned upon.
I know he posted the whole thing, but I already did like 75% of the question probably more. He just helped the rest I needed help with. But I don't really understand the rules, this is in 'Turing Help' section, but you can't help him fully. That's kind of gay. |
|
|
|
|
|
Euphoracle
|
Posted: Thu Nov 06, 2008 3:33 pm Post subject: RE:Grocery Problem |
|
|
No, it's not a sexuality, and it makes perfect sense, because you learn nothing. |
|
|
|
|
|
Jurica
|
Posted: Thu Nov 06, 2008 3:55 pm Post subject: Re: RE:Grocery Problem |
|
|
Euphoracle @ Thu Nov 06, 2008 3:33 pm wrote: No, it's not a sexuality, and it makes perfect sense, because you learn nothing.
I don't mean gay as in Homosexual, and it doesn't really because if I need help people help me, they may be giving me the whole answer but I did most of the question I just needed help with a couple of things, and I do learn something. I didn't just copy and paste the solution and pasted it and handed my work. I went through it and read it so next time I need to do something like that again I'll know what to do, so I basically do learn. And why would they make a 'Turing Help' page if you're not even aloud to really help them? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jbking
|
Posted: Thu Nov 06, 2008 4:00 pm Post subject: RE:Grocery Problem |
|
|
The question is what kind of help to give: If the whole answer is given, some may learn and some may not learn from it. Usually there are ways to suggest various ways to try to solve the problem so that the methodology is transferred but not everyone can get that.
It would be one thing if this was called "Turing Solutions" but it isn't. |
|
|
|
|
|
Jurica
|
Posted: Thu Nov 06, 2008 4:09 pm Post subject: RE:Grocery Problem |
|
|
I know he gave the whole solution but I asked to help me with a couple of things, he just used my answers that I've posted changed it a bit to put the stuff I asked. This isn't supposed to help other people it's for me, I asked him. |
|
|
|
|
|
Geniis
|
Posted: Thu Nov 06, 2008 4:19 pm Post subject: Re: Grocery Problem |
|
|
sorry i didnt want to give him the entire thing but i didnt know how else to help.. what i posted was only supposed to give him a general idea of what to do...
I guess before i help someone i should make sure i know how to explain things first eh? lol |
|
|
|
|
|
Jurica
|
Posted: Thu Nov 06, 2008 4:21 pm Post subject: RE:Grocery Problem |
|
|
Lol, true. But thanks anyways. |
|
|
|
|
|
|
|