Posted: 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
var numberitems : int := 0
var item : flexible array 1 .. numberitems of string
var amountitem : flexible array 1 .. numberitems of int
var priceitem : flexible array 1 .. numberitems of real
var answer, pass1, pass2, password, today, restock, login : string
var passinput : string (1)
%%%%%%%%%%%%%%%%START OF NIGHT%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%PASSWORD, LOGIN AND RESTOCK LOGIC%%%%%%%%%%%%%%%%%%%%%%
loop
exit when password = pass1 or password = pass2
password := ""
put "Enter your password."
loop
getch (passinput)
if passinput = (KEY_ENTER) then
exit
end if
put "*" ..
password += passinput
end loop
cls
locate (1, 1)
if password = pass1 then
login := "Jack"
put "Welcome Jack."
loop
put "Are you restocking the canteen? (y/n)"
get restock
if restock = "y" or restock = "Y" or restock = "yes" or restock = "Yes" then
put "How many items are you going to sell?"
get numberitems
new item, numberitems
new priceitem, numberitems
new amountitem, numberitems
for x : 1 .. numberitems
put "Ok, what are you selling?"
get item (x) : *
put "Enter the amount of \"", item (x), "\" you're going to put in."
get amountitem (x)
put "How much will it sell for? (Omit the $ sign)"
get priceitem (x)
end for
cls
put "You are going to sell: " ..
for x : 1 .. numberitems
put amountitem (x), " ", item (x), " for ", priceitem (x) : 1 : 2, "$"
end for
put "Are you ready to go? (y/n)"
loop
get answer
if answer = "y" or answer = "Y" or answer = "yes" or answer = "Yes" then
exit
elsif answer = "n" or answer = "N" or answer = "no" or answer = "No" then
put "Take your time, write it down somewhere."
delay (1000)
put "Are you ready to go? (y/n)"
else
put "Are you ready? Press \"y\" or \"n\" followed by enter."
end if
end loop
exit
elsif restock = "n" or restock = "N" or restock = "No" or restock = "no" then
put "Ok, you're good to go. You will get a report at the end of the night."
exit
else
put "Are you restocking? (y/n)"
end if
end loop
elsif password = pass2 then
login := "John"
put "Welcome John, good luck tonight!"
exit
end if
end loop
cls
put "You have: "
for x : 1 .. upper (item)
put amountitem (x), " ", item (x), " selling for ", priceitem (x) : 1 : 2
end for
put "What's the Float?"
get float
%%%%%%%%%%%%%BUTTONS AND COUNTER%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cls
for x : 1 .. upper (item)
for y : 1 .. (upper (item) - 1)
color (green)
locate ((maxrow - 2), 2 + (y * 10))
put item (x), ": " ..
end for
end for
Can somebody hint me in the right directions as to how am I going to be able to make enough buttons for the user?
Sponsor Sponsor
TokenHerbz
Posted: Mon Jul 31, 2006 6:55 pm Post subject: (No 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
Clayton
Posted: Mon Jul 31, 2006 8:19 pm Post subject: (No 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!
CooKieLord
Posted: Tue Aug 01, 2006 3:07 pm Post subject: (No 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,
code:
var button: flexible array 1..numberitems of int
And later on do something like
code:
for x: 1..numberitems
button(x):= GUI.CreateButton (10+(x*20), 10, 100, item, countitem)
end for
Would that be effective? *Hurries and tries it*[/code]
NikG
Posted: Tue Aug 01, 2006 6:08 pm Post subject: (No 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.
Clayton
Posted: Tue Aug 01, 2006 7:26 pm Post subject: (No 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,
code:
var button: flexible array 1..numberitems of int
And later on do something like
code:
for x: 1..numberitems
button(x):= GUI.CreateButton (10+(x*20), 10, 100, item, countitem)
end for
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
Clayton
Posted: Tue Aug 01, 2006 7:30 pm Post subject: (No subject)
oh and another thing, like NikG said, you shouldnt have "numberitems" in your flexy array declaration, have it something like this:
Turing:
var myArray :flexiblearray1..0ofint
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 )
CooKieLord
Posted: Sun Aug 06, 2006 6:34 am Post subject: (No subject)
Ok thanks for the help, I'll try it that way.
Sponsor Sponsor
CooKieLord
Posted: Sun Aug 06, 2006 6:45 am Post subject: (No 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.
code:
var numberitems : int := 0
var button : flexible array 1..0 of int
var item : flexible array 1 .. 0 of string %somewhere along the line this gets resized, I left it out for the sake of focusing)
new button, numberitems+1
for x : 1..upper(item)
button(x):= GUI.CreateButton ((10+ (100*x)+5),10,100,item(x), countitem)
end for
loop
exit when GUI.ProcessEvent
end loop
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?
CodeMonkey2000
Posted: Sun Aug 06, 2006 10:12 pm Post subject: (No 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.
code:
proc add (a : int, b : int) %taks in 2 integer values
delay (100)
put a + b
end add
for x : 1 .. 10
for decreasing y : 10 .. 1 by 1
add (x, y)% gives add 2 int values
end for
end for
Cervantes
Posted: Mon Aug 07, 2006 9:47 am Post subject: (No 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?
code:
function add (a, b : int) : int
result a + b
end add
This allows us to do things like this:
code:
add (5, add (7, 8))
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:
code:
CodeMonkey2000
Posted: Mon Aug 07, 2006 7:44 pm Post subject: (No 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.
Cervantes
Posted: Mon Aug 07, 2006 8:38 pm Post subject: (No 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.
TokenHerbz
Posted: Mon Aug 07, 2006 10:48 pm Post subject: (No subject)
id rather go threw the toubles of creating a button , then im sure you can call a function with it, tho it might not be as nice
Clayton
Posted: Tue Aug 08, 2006 12:49 pm Post subject: (No 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?