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

Username:   Password: 
 RegisterRegister   
 Problem with looping. I need it to output a list of parts.
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HelloWorld




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Nov 05, 2004 2:22 pm   Post subject: (No 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.
HelloWorld




PostPosted: Fri Nov 05, 2004 2:30 pm   Post subject: (No 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.
HelloWorld




PostPosted: 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.
wtd




PostPosted: Fri Nov 05, 2004 3:17 pm   Post subject: (No 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.
HelloWorld




PostPosted: Fri Nov 05, 2004 10:08 pm   Post subject: (No 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.
wtd




PostPosted: Fri Nov 05, 2004 10:18 pm   Post subject: (No 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
HelloWorld




PostPosted: Fri Nov 05, 2004 10:47 pm   Post subject: (No 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!
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Nov 05, 2004 10:56 pm   Post subject: (No subject)

You're quite welcome.
HelloWorld




PostPosted: Fri Nov 05, 2004 11:04 pm   Post subject: (No 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.
wtd




PostPosted: Fri Nov 05, 2004 11:09 pm   Post subject: (No 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.
HelloWorld




PostPosted: Fri Nov 05, 2004 11:34 pm   Post subject: (No 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
wtd




PostPosted: Fri Nov 05, 2004 11:42 pm   Post subject: (No 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.
HelloWorld




PostPosted: Sat Nov 06, 2004 12:49 am   Post subject: (No 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.
wtd




PostPosted: Sat Nov 06, 2004 12:57 am   Post subject: (No subject)

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


What exactly is that?
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 2  [ 24 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: