Computer Science Canada Using Arrays to create a user-defined number of buttons |
Author: | CooKieLord [ Mon Jul 31, 2006 4:58 pm ] | ||||
Post subject: | Using Arrays to create a user-defined number of buttons | ||||
Hi every, I was making a little program to help track the sales of a canteen that I work with. At first, just to get the concept, I predefined the amount of items you can sell and the price, but now I decided I was close enough so I should change it so that the user enters the amount of items, how many of each and the price. And by using flexible arrays (I just recenly discovered them), I'm hoping to be on the right track. What stumped me was how I was going to go and make numbers of buttons equal to the amount of items the user would need, because the program will use a graphic interface to track the sales (Push the button and you just sold something). I also included a password system so only the person in charge of the stock and sales can access the inventory (I plan to eventually implement the feature of saving the inventory, sales, etc. to be re-used from week to week). For now, just use
Here is my code:
Can somebody hint me in the right directions as to how am I going to be able to make enough buttons for the user? |
Author: | TokenHerbz [ Mon Jul 31, 2006 6:55 pm ] |
Post subject: | |
learn class's i think it would be easier this way, say you have a class of buttons: each new button you add, you add one to the flexible array ^buttons, and can set specific info for it. if you sont want to learn class's... Try useing a flexible array in a way the user can access it. you code about is a mess and i cant run it at all.. try to get a loop and use mousewhere to locate a button drawn, so u know if you clicked add items or sell, or even if you use your GUI buttons, then go into an if or call a proc to make changes to your array as needed... Im not really sure what your trying to do, so if i make no sence just explain yourself better :S:S:S:S:S:S |
Author: | Clayton [ Mon Jul 31, 2006 8:19 pm ] |
Post subject: | |
well you know that buttons are an int right? so just have some sort of procedure or function that figures out how many items there are, updates a flexible array with how many items you have, then creates a button for each, and voila! |
Author: | CooKieLord [ Tue Aug 01, 2006 3:07 pm ] | ||||
Post subject: | |||||
SuperFreak82 wrote: well you know that buttons are an int right? so just have some sort of procedure or function that figures out how many items there are, updates a flexible array with how many items you have, then creates a button for each, and voila!
So if I were to make a new flxible array,
And later on do something like
Would that be effective? *Hurries and tries it*[/code] |
Author: | NikG [ Tue Aug 01, 2006 6:08 pm ] |
Post subject: | |
Just a note: if you're going to declare the variable after you know what numberitems is, then you don't need to declare the array as a flexible array. In the case where this will be looped (meaning you need a flexy array), you should declare the variable at the top of your code and use the new command to resize the array. |
Author: | Clayton [ Tue Aug 01, 2006 7:26 pm ] | ||||
Post subject: | |||||
CooKieLord wrote: SuperFreak82 wrote: well you know that buttons are an int right? so just have some sort of procedure or function that figures out how many items there are, updates a flexible array with how many items you have, then creates a button for each, and voila!
So if I were to make a new flxible array,
And later on do something like
Would that be effective? *Hurries and tries it* That would be pretty much all you need to do, just make sure that you have enough room in your flexy array before trying to create a button (by that i mean that you have your new statement preferably before you create the button) that way you dont end up trying to fit too much info into your array ![]() |
Author: | Clayton [ Tue Aug 01, 2006 7:30 pm ] | ||
Post subject: | |||
oh and another thing, like NikG said, you shouldnt have "numberitems" in your flexy array declaration, have it something like this:
then whenever you go to create a button just before you actually create it, use the new command to resize the array to hold one more element than before, to allow for the button. If you have not done so already, check out the Turing Walkthrough for the tutorial on flexible arrays, and look through the other great tuts in there too:D (Sorry Bout the double post ![]() |
Author: | CooKieLord [ Sun Aug 06, 2006 6:34 am ] |
Post subject: | |
Ok thanks for the help, I'll try it that way. |
Author: | CooKieLord [ Sun Aug 06, 2006 6:45 am ] | ||
Post subject: | |||
Pardon the double post (we really need the Edit button here) But the buttons works, now it's a matter of setting which procedure each button will do. Since I have it in a for loop, it would make sense to use an array, yet again, to set the procedures.
Now what I need is that each button will deduct the amount of stock that's left. Let's say I make it so theres two buttons: Pop and Chips. When I click on Pop, I want it to do item(1)-=1, and for Chips it would be item(2)-=1. So would I need two different procedures? If so, how could I make it so that it assigns the correct procedure to the buttons? |
Author: | CodeMonkey2000 [ Sun Aug 06, 2006 10:12 pm ] | ||
Post subject: | |||
im not sure what you want, but if you want to add 2 different prices using only one procedure, id hav that procedure take in an argument.
|
Author: | Cervantes [ Mon Aug 07, 2006 9:47 am ] | ||||||
Post subject: | |||||||
First, I can't imagine why you would want to put a delay in your add procedure. Every time you want to add two things, you've got to wait 100ms? That just doesn't make sense. If for some reason you want to wait after or before you've added something, call the delay with some specified value (not necessarily 100ms) after or before calling your procedure. Now, more importantly, why would you use a procedure? A function suits much better, since a function actually returns a value, whereas with this procedure you're forced into outputting your value. What if you don't want to output it? What if you want to add this sum to another value?
This allows us to do things like this:
That would return 5 + (7 + 8), which is 20. Now we ask, why would we even want to make a function to add two numbers? Clearly, 5 + 7 + 8 is more succinct and natural than add (5, add (7, 8)). So we've first eliminated your delay, then turned your procedure into a function, then eliminated the function. Our final code:
![]() |
Author: | CodeMonkey2000 [ Mon Aug 07, 2006 7:44 pm ] |
Post subject: | |
do buttons even allow you to use functions? i didnt use the code with guicreatebutton cause i forgot how to use buttons, and was too lazy to look it up. and about the delay, i dont know what i was thinking. |
Author: | Cervantes [ Mon Aug 07, 2006 8:38 pm ] |
Post subject: | |
Oh, sorry. I didn't realize this was for a GUI thing. Yeah, you can only use 'action procedures' with buttons, which means a procedure with no parameters. It sucks. |
Author: | TokenHerbz [ Mon Aug 07, 2006 10:48 pm ] |
Post subject: | |
id rather go threw the toubles of creating a button ![]() |
Author: | Clayton [ Tue Aug 08, 2006 12:49 pm ] |
Post subject: | |
see the thing i dont understand with creating your own buttons (or any GUI for that matter) is how to be able to go to a procedure or function with a variable number of parameters, how would you go about that? |
Author: | Windsurfer [ Tue Aug 08, 2006 1:18 pm ] | ||
Post subject: | |||
SuperFreak82 wrote: see the thing i dont understand with creating your own buttons (or any GUI for that matter) is how to be able to go to a procedure or function with a variable number of parameters, how would you go about that?
I seem to remember something in OOP about polymorphism... is that what we're looking for? I know i'm starting to learn a bit about Java, and you can have multiple funtions named the same to accept different number of parameters and types of parameters. Or am i just confusing people and not helping? Probably not helping. Or, are you asking about how funtions and procedures can accept an array of varying length? That, i know how to do.
Addmittedly, not the most universal function... but it works. You can add on more numbers in the init() part, without changing any other code, and it will add them all. |
Author: | Clayton [ Tue Aug 08, 2006 1:28 pm ] |
Post subject: | |
i wasnt asking about arrays of varying length, i was asking about a variable number of parameters as for functions with same names but different numbers of parameters, alas you cannot do that in Turing ![]() |
Author: | [Gandalf] [ Tue Aug 08, 2006 2:32 pm ] |
Post subject: | |
No, you cannot overload functions in Turing. I think the answer you're looking for might be an array of procedures. Each procedure would go under the same name (just with a different index) and they could do completely different things (including taking a varying amount of parameters). I'd help you more with an example, but I forget the exact syntax. If you think this would solve your problem, I'm sure Clevernuts will be able to help you more. ![]() |
Author: | [Gandalf] [ Tue Aug 08, 2006 2:49 pm ] | ||||
Post subject: | |||||
Guess not. I decided to check it out, and it seems you can't have different parameters:
Will not work, since the array was declared as an array of procedures with no parameters. Something like this, on the other hand will work:
Stupid Turing. ![]() |
Author: | Cervantes [ Tue Aug 08, 2006 4:42 pm ] | ||
Post subject: | |||
[Gandalf] wrote: Guess not. I decided to check it out, and it seems you can't have different parameters:
Exactly. And that's the precise reason why you can't give parameters to your procedures for a GUI widget. In the source code, the type of the variable that holds these procedures has to be consistant throughout all the widgets. And so they opted for a procedure with no parameters, which is really the best option available. If you want to use a procedure with parameters, the best thing you can do is to declare it just as you wanted it, then create more procedures (one for each widget) that call the initial procedure with whatever parameters you want. For example,
|
Author: | Clayton [ Tue Aug 08, 2006 5:56 pm ] |
Post subject: | |
so is it actually possible in any way to create your own GUI that doesnt use action procedures? |
Author: | Cervantes [ Tue Aug 08, 2006 8:48 pm ] |
Post subject: | |
Absolutely. I did it, for example. The way I did it was to have a boolean function return true when the widget is activated. So when a button is clicked, the function returns true. At all other times, it returns false. You can then have a loop that runs this function in an if statement, with the code to be executed when the widget is activated contained within the if statement. For more complex widgets with multiple states, the function could return an integer: 0 for unactivated, some number greater than 0 for an activated state, with each integer (or perhaps in powers of 2 for some or magic) specifying a particuler state. |