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.
Then, I want it to output a chart/table that will list each part. Heres the code for it:
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:
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. |
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:
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:
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:
|
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....
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...
"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:
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:
|
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...
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.
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 [/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. |