Computer Science Canada

Problem with looping. I need it to output a list of parts.

Author:  HelloWorld [ Fri Nov 05, 2004 1:58 pm ]
Post subject:  Problem with looping. I need it to output a list of parts.

As you will notice, I am a newbie at Turing. I started using it last week in my grade 10 computer tech class. Anyway, we have written many basic programs in the past week.

Now, we have our first assignment, it is to make a program for the grade 9 automotive department. The program is to replace their old pen and paper recepts with an easy to use program.

I have the whole program pretty much finish, I'm just stuck at one part.

Heres the code, this simply loops for the amount of parts purchased.

code:

put "How many parts were purchased? " ..
get n

for counter : 1 .. n

    put "What is the part number? " ..
    get part

    put "How many of this part was purchased? " ..
    get quantity

    put "Give a short description for this part: " ..
    get description

    put "What is the Net price of this part? " ..
    get netprice

    put "What is the Unit price for this part? " ..
    get price

    put "", skip

    put "Press any key to continue." ..
    getch (reply)
    cls

end for



Then, I want it to output a chart/table that will list each part. Heres the code for it:

code:

put "Quantity\tPart#\t\tPart Description\tUnit Price\tNet Price"

for counter : 1 .. n
    put "", quantity, "\t\t", part, " \t\t", description, "\t\t\t", price, "\t\t", netprice
end for



See, it loops the same part over and over, no matter what you input as the second, third, or whatever part. See what I mean?

Well, just in case if you guys don't understand, heres the full program:

code:

import GUI in "%oot/support/lib/GUI"  %Needed to use buttons.

setscreen ("graphics:800;550") %Size of screen and title

%----------------------VARIABLES-------------------------------
const gst := 0.07
const pst := 0.08
const tax := 1.15
var n : int %How many parts; how many loops.
var quantity, price, netprice, sub, total : real
var reply : string (1) %variable to use with getch command
var name, tel, dat, make, year, mile, license, col, serial, inspect, tobe, need, part, description : string
%--------------------------------------------------------------



%----------------------Program info/intro----------------------------
put "" : 35, "GDHS TRANSPORTATION DEPARTMENT"
put "", skip
put "This program was made for the automotive department to create a simple recept for the customer(s)."
put "*DO NOT PUT ANY SPACES, AS IT CAN MESS UP THE PROGRAM!"
put "You will be promt with many questions.  Please answer all of them."

locatexy (200, 200)
put "WHEN READY; PRESS ANY KEY" ..
getch (reply)
cls
%--------------------------------------------------------------



%--------------Values for each Variable------------------------
put "Please fill in all the blanks."
put "If you don't know the answer, type 'UNKNOWN'."
put "", skip
put "Customers Name: " ..
get name

put "Telephone #: " ..
get tel

put "Current Date (m/d/y): " ..
get dat

put "Vehicle Make: " ..
get make

put "Year: " ..
get year

put "Mileage: " ..
get mile

put "License #: " ..
get license

put "Colour: " ..
get col

put "Serial #: " ..
get serial

put "Inspected by: " ..
get inspect

put "", skip

put "Work to be done: " ..
get tobe

put ""

put "Work needed: " ..
get need

put "", skip

put "Press any key to continue to the next set of questions." ..
getch (reply)
cls

put "Please fill in all the blanks."
put "If you don't know the answer, type 'UNKNOWN'."
put "", skip

put "How many parts were purchased? " ..
get n

for counter : 1 .. n

    put "What is the part number? " ..
    get part

    put "How many of this part was purchased? " ..
    get quantity

    put "Give a short description for this part: " ..
    get description

    put "What is the Net price of this part? " ..
    get netprice

    put "What is the Unit price for this part? " ..
    get price

    put "", skip

    put "Press any key to continue." ..
    getch (reply)
    cls

end for
%--------------------------------------------------------------



%-------------------OUTPUT.......--------------------
put ""
put "" : 25, "GDHS TRANSPORTATION DEPARTMENT"
put "", skip

put "\t", "Customers Name: ", name, "\t\t\t", "Telephone #: ", tel

put "\t", "Date: ", dat, "\t\t\t", "Vehical Make: ", make

put "\t", "Year: ", year, "\t\t\t", "Mileage: ", mile

put "\t", "License #: ", license, "\t\t\t", "Colour: ", col

put "\t", "Serial #: ", serial, "\t\t\t", "Inspected by: ", inspect

put ""
put "Work to be done: ", tobe
put ""

drawline (0, 0, 0, 0, 0) %For over top of work needed.

put "Work needed: ", need
put "", skip


put "Quantity\tPart#\t\tPart Description\tUnit Price\tNet Price"

for counter : 1 .. n
    put "", quantity, "\t\t", part, " \t\t", description, "\t\t\t", price, "\t\t", netprice
end for

put "", skip


put "Sub Total $: ", price * quantity
put "GST $: ", gst * price * quantity
put "PST $: ", pst * price * quantity
put "Total $: ", price * quantity * tax

put "", skip
put "", skip
put "" : 5, "Paid: " : 35, "Customer Signature : "

drawline (0, 0, 0, 0, 0) %For under paid and customer signature.
%--------------------------------------------------------------



%---------------------The QUIT button--------------------------
procedure QuitPressed
    GUI.Quit
end QuitPressed

var quitButton := GUI.CreateButton (500, 10, 100, "Quit", QuitPressed) %VARIABLE

loop
    exit when GUI.ProcessEvent
end loop

var quitMessage := GUI.CreateLabelFull (0, 0, "Thank You For Using This Program!",
    maxx, maxy - 135, GUI.CENTER + GUI.MIDDLE, 0) %quit message
%--------------------------------------------------------------



I would be extremely greatfull to anyone that would be kind enough to help me. Also, if possible, I was thinking of inplementing a restart button at the bottom beside the quit button. And when the user clicked the restart button, the entire program would restart. If someone knows how I would go about doing that. It would help me a lot!

Thank you all in advance. Smile

Author:  wtd [ Fri Nov 05, 2004 2:22 pm ]
Post subject: 

This is because you only have one variable you're putting each bit of data into. The first time around you put the part number into "part", and the second time around you do the same thing, which wipes out the first bit of data.

Arrays tutorial.

Author:  HelloWorld [ Fri Nov 05, 2004 2:30 pm ]
Post subject: 

Thank you for the quick reply, hm, I understand what you're saying, but I'm still having trouble. First time using an array, I'll keep trying.

Author:  HelloWorld [ Fri Nov 05, 2004 3:11 pm ]
Post subject:  More help, sorry.

Since I have started this program, I have spent now well over two hours, and I'm starting to get a little confused, exspecially with this array thingie. Could someone please help me out some more?

Heres what I have now:

code:

import GUI in "%oot/support/lib/GUI"  %Needed to use buttons.

setscreen ("graphics:800;550") %Size of screen and title

%----------------------VARIABLES-------------------------------
const gst := 0.07
const pst := 0.08
const tax := 1.15
var n : int %How many parts; how many loops.
var sub, total : real
var reply : string (1) %variable to use with getch command
var part, description : array 1 .. n of string
var quantity, price, netprice : array 1 .. n of int
var name, tel, dat, make, year, mile, license, col, serial, inspect, tobe, need : string
%--------------------------------------------------------------



%----------------------Program info/intro----------------------------
put "" : 35, "GDHS TRANSPORTATION DEPARTMENT"
put "", skip
put "This program was made for the automotive department to create a simple recept for the customer(s)."
put "*DO NOT PUT ANY SPACES, AS IT CAN MESS UP THE PROGRAM!"
put "You will be promt with many questions.  Please answer all of them."

locatexy (200, 200)
put "WHEN READY; PRESS ANY KEY" ..
getch (reply)
cls
%--------------------------------------------------------------



%--------------Values for each Variable------------------------
put "Please fill in all the blanks."
put "If you don't know the answer, type 'UNKNOWN'."
put "", skip
put "Customers Name: " ..
get name

put "Telephone #: " ..
get tel

put "Current Date (m/d/y): " ..
get dat

put "Vehicle Make: " ..
get make

put "Year: " ..
get year

put "Mileage: " ..
get mile

put "License #: " ..
get license

put "Colour: " ..
get col

put "Serial #: " ..
get serial

put "Inspected by: " ..
get inspect

put "", skip

put "Work to be done: " ..
get tobe

put ""

put "Work needed: " ..
get need

put "", skip

put "Press any key to continue to the next set of questions." ..
getch (reply)
cls

put "Please fill in all the blanks."
put "If you don't know the answer, type 'UNKNOWN'."
put "", skip

put "How many parts were purchased? " ..
get n

for counter : 1 .. n

    put "What is the part number? " ..
    get part (n)

    put "How many of this part was purchased? " ..
    get quantity (n)

    put "Give a short description for this part: " ..
    get description (n)

    put "What is the Net price of this part? " ..
    get netprice (n)

    put "What is the Unit price for this part? " ..
    get price (n)

    put "", skip

    put "Press any key to continue." ..
    getch (reply)
    cls

end for
%--------------------------------------------------------------



%-------------------OUTPUT.......--------------------
put ""
put "" : 25, "GDHS TRANSPORTATION DEPARTMENT"
put "", skip

put "\t", "Customers Name: ", name, "\t\t\t", "Telephone #: ", tel

put "\t", "Date: ", dat, "\t\t\t", "Vehical Make: ", make

put "\t", "Year: ", year, "\t\t\t", "Mileage: ", mile

put "\t", "License #: ", license, "\t\t\t", "Colour: ", col

put "\t", "Serial #: ", serial, "\t\t\t", "Inspected by: ", inspect

put ""
put "Work to be done: ", tobe
put ""

drawline (0, 0, 0, 0, 0) %For over top of work needed.

put "Work needed: ", need
put "", skip


put "Quantity\tPart#\t\tPart Description\tUnit Price\tNet Price"

for counter : 1 .. n
    put "", quantity (n), "\t\t", part (n), " \t\t", description (n), "\t\t\t", price (n), "\t\t", netprice (n)
end for

put "", skip


put "Sub Total $: ", price (n) * quantity (n)
put "GST $: ", gst * price (n) * quantity (n)
put "PST $: ", pst * price (n) * quantity (n)
put "Total $: ", price (n) * quantity (n) * tax

put "", skip
put "", skip
put "" : 5, "Paid: " : 35, "Customer Signature : "

drawline (0, 0, 0, 0, 0) %For under paid and customer signature.
%--------------------------------------------------------------



%---------------------The QUIT button--------------------------
procedure QuitPressed
    GUI.Quit
end QuitPressed

var quitButton := GUI.CreateButton (500, 10, 100, "Quit", QuitPressed) %VARIABLE

loop
    exit when GUI.ProcessEvent
end loop

var quitMessage := GUI.CreateLabelFull (0, 0, "Thank You For Using This Program!",
    maxx, maxy - 135, GUI.CENTER + GUI.MIDDLE, 0) %quit message
%--------------------------------------------------------------



Yes, I know reading over lots of code is very long and boring. I hope someone can help me figure this out, yes I am a complete newbe.

I'll keep checking back here to see if anyone has tried to help me, but I'll also keep trying to figure it out myself at the same time.

Author:  wtd [ Fri Nov 05, 2004 3:17 pm ]
Post subject: 

Take things one step at a time and they'll become more manageable.

First thing: when you declare an array like so:

code:
var foo : array 1 .. n of Wooble


You have to know the value of n, before you declare the array.

Author:  HelloWorld [ Fri Nov 05, 2004 10:08 pm ]
Post subject: 

wtd wrote:
You have to know the value of n, before you declare the array.


I know that, but I want the user it input the value of 'n'. I don't know exactly what to do, I'm really stuck. I tried declaring the arrays after the user inputs the value for 'n', but then it runs like it did in the beginning, looping the same things over and over again.

And that array tutorial doesn't mension anything like this, I suppose that its probably something very simple. Well, I'll keep experimenting unless someone could help me some more.

Oh, and by the way, I appreciate the help that you have already given me, wtd. Thanks.

Author:  wtd [ Fri Nov 05, 2004 10:18 pm ]
Post subject: 

A simple example to get you started:

code:
var n : int

put "Number of numbers?"
get n

var numbers : array 1 .. n of int

for counter : 1 .. n
    put "Number?"
    get numbers (counter)
end for

for counter : 1 .. n
    put numbers (counter)
end for

Author:  HelloWorld [ Fri Nov 05, 2004 10:47 pm ]
Post subject: 

Woah! Thank you so much, you truely are a Programming God!

I like the way to help out, you don't simply give the exact answer, you make people work to figure out their problems, therefore they(I) learn. Thank you.

I'll mess around with this. And, I think I understand now, thanks again!

Author:  wtd [ Fri Nov 05, 2004 10:56 pm ]
Post subject: 

You're quite welcome.

Author:  HelloWorld [ Fri Nov 05, 2004 11:04 pm ]
Post subject: 

I got it, its all working, thank you so much!

I know you already help me a lot, but if you have any idea of how to make that restart button that I was speaking of in my first post. I would appreciate any help at all.

Author:  wtd [ Fri Nov 05, 2004 11:09 pm ]
Post subject: 

Put your entire program in a loop. At the end of the loop, ask if the user wants to quit. If their input indicates they want to quit, then exit the loop.

Author:  HelloWorld [ Fri Nov 05, 2004 11:34 pm ]
Post subject: 

Sounds so much easier when you say it, than it seemed when I was trying to figure it out. lol. Very easy, did it, done that. Thanks.

Another question, I don't know why my subtotal and total and all that aren't adding up right....

code:

put "Sub Total $: ", price (n) * quantity (n)
put "GST $: ", gst * price (n) * quantity (n)
put "PST $: ", pst * price (n) * quantity (n)
put "Total $: ", price (n) * quantity (n) * tax


I tried replacing the 'n' with 'counter' but it causes an error. With 'n', it only adds up the price of the last part.
--

Also one more question, if you don't mind. Um, how can I make it so more than one word can be typed for a variable.

For example, the problem right now is: when it asks for 'Customers Name:' If I were to input 'John Doe', it would take 'John' as the customers name and and then 'Doe" as the input for the second question, which would be 'Telephone #:'.

Is it just a matter of the type of variable, or what do I have to change? Right now, most of my variables are strings.
--

Thanks again in advance!

* +10 bits

Author:  wtd [ Fri Nov 05, 2004 11:42 pm ]
Post subject: 

Your second question is easier. You can find the answer by consulting the Turing reference (press F10).

Go to:

Turing Language -> Language Elements -> get - file statement

Look up the section on line-oriented input.

As for your first question:

In your code, "n" is the number of parts you're inputting information about.

When you loop over that array, with, say...

code:
for counter : 1 .. n
   % do something
end for


"counter" is the variable the actual number gets stored in each time. "n" is always the same.

Author:  HelloWorld [ Sat Nov 06, 2004 12:49 am ]
Post subject: 

Well, I got the ' get word : * ', but I am still stuck with my other problem. All I have is this:
code:

for counter : 1 .. n
    put "Sub Total $: ", price (counter) * quantity (counter)
    put "GST $: ", gst * price (counter) * quantity (counter)
    put "PST $: ", pst * price (counter) * quantity (counter)
    put "Total $: ", price (counter) * quantity (counter) * tax
end for


It runs without errors, but it doesnt't do what I want, it does the same exact thing as before, but it is looped for 'n' times.

Author:  wtd [ Sat Nov 06, 2004 12:57 am ]
Post subject: 

HelloWorld wrote:
It runs without errors, but it doesnt't do what I want


What exactly is that?

Author:  HelloWorld [ Sat Nov 06, 2004 1:12 am ]
Post subject: 

Okay, I had something messed up, now its running so it shows the Sub Total, GST, PST and Total for each item, and thats great, but what I really want is, one Subtotal, GST, PST, and Total for all of the parts....

Author:  wtd [ Sat Nov 06, 2004 1:19 am ]
Post subject: 

Ok... a simple demo:

code:
var amounts : array 1 .. 4 of int
var total : int := 0

for counter : 1 .. 4
   total += amounts (counter)
end for

Author:  HelloWorld [ Sat Nov 06, 2004 1:28 am ]
Post subject: 

Well, I don't know if you meant for that to run or not, but it doesn't.

Anyway, sorry, but I looked that over a few times, and I have no idea what to do with it. (I don't really know if it matters, but just for you're own info in case some things don't work with older versions, I am using Turing 4.0.1)

Author:  wtd [ Sat Nov 06, 2004 1:32 am ]
Post subject: 

Well... to give the array some values...

code:
var amounts : array 1 .. 4 of int := init(1, 2, 3, 4)
var total : int := 0

for counter : 1 .. 4
   total += amounts (counter)
end for

put amount


Your array would already be populated with values.

Author:  HelloWorld [ Sat Nov 06, 2004 1:50 am ]
Post subject: 

I don't think I understand, but let me say what I am thinking anyway..

So, the array 'amounts' is a varriable that would hold or store the value of each parts price.....

This is complicated for me.... Could you give me some more detail on how to implement this into my code? And tell me whether or not I am even close to understanding the simple demo....

Author:  wtd [ Sat Nov 06, 2004 3:45 am ]
Post subject: 

In the simple demo I posted, we first create an array.

This is a single variable which stores (in this case) 4 integers. By default, those are 1, 2, 3 and 4.

Next we create an integer variable called "total", and we give it a starting value of zero.

Then, we loop over the contents of the array, and we add each number in the array to "total".

When the loop is done, we have the total.

Apply this to your array holding the prices of items, and you can find the total price.

Author:  HelloWorld [ Sun Nov 07, 2004 1:11 pm ]
Post subject: 

Thanks again. Right after reading you're last post, it was so much more clear in my head, and I figured it out in a few seconds after..... Thanks.

Author:  myob [ Mon Nov 08, 2004 7:45 am ]
Post subject: 

just a thought to make it fancier

declare a record then declare the array on the record. it should look something like this, i dont have the program with me so if any mistake just look it up on F1

also sometimes u need to declare ur array before u get the number. like sometimes u ask "how many u want?" inside a loop but u dont wanna declare it after that cuz then the loop just comes back again and redeclare the variable, ends up erasing the whole array.

so alternatively u can use flexible array.

code:

type record price
tagprice:real
gst:real
pst:real
end record %(or maybe "end price", more confident about "end record" though)

var price_all : flexible array 1 .. 0 of tagprice
var n : int
put "how much u want?"
get n

new price_all,n %this command renews the size of the flexible array

for i : 1 .. n
put "what's the price?"
get price_all.tagprice(i)
price_all.gst(i):=price_all.tagprice(i) * 0.08
price_all.pst(i):=price_all.tagprice(i) * 0.07
end for


one reason to to declare array with a range of 1 .. 0 because u dont want it to take up any ram before u actually putting values into it. that is just one exploit on flexible array Smile[/code]

Author:  HelloWorld [ Mon Nov 08, 2004 11:08 am ]
Post subject: 

Okay, I see what you're saying, I'll see if I can work some of that into my code.


: