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

Username:   Password: 
 RegisterRegister   
 Arrays In A Cashier Program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. T




PostPosted: Tue Apr 05, 2005 11:36 pm   Post subject: Arrays In A Cashier Program

In my cashier program i declared the array
code:
var foodItems : array 1 .. 5 of string

and i inserted five foods into the array.
Each food has a corresponding price.
How would I go about allowing the user to select a pre-existing food name in order that he/she can change its price?
HERE IS MY CODE AS OF YET
(i realize i havent outputed my intro, just ignore it for now)
----------------------------------
code:
%%%%%%%%%%%
% CASHIER %
%%%%%%%%%%%%%%%
% BLING-BLING %
%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Variable Declaration
colour (0)
colourback (7)
cls
View.Set ("offscreenonly")
var slidingTitle : int := 0
var titleFont, shopFont : int
titleFont := Font.New ("serif:80")
shopFont := Font.New ("serif:20")
var x, y, button : int
var increase :int :=5
var foodItems : array 1 .. 5 of string
foodItems (1):="CheeseBlintz"
foodItems (2):="Corn Muffin"
foodItems (3):="Cactus Juice"
foodItems (4):="Moldy Cheese"
foodItems (5):="Liver Shnitzel"
var foodPrices : array 1..5 of real
foodPrices (1):=4.59
foodPrices (2):=5.49
foodPrices (3):=9.99
foodPrices (4):=15.01
foodPrices (5):=20.33
var listOrder :int:=3
var answer :int
var addItems, addPrices:int
var foodNames :string
%Intro Procedure
%note: View.Update was the best way to avoid streaking and flickering
procedure intro
for title : 1 .. 500
    colour (0)
    colourback (7)
    cls
    Font.Draw (" Jane & Finch", title - 500, 300, titleFont, brightred)
    %%
    Font.Draw ("  Super Store ", 500 - title, 100, titleFont, brightred)
    View.Update
end for
View.Set ("nooffscreenonly") %end of animations, output will be drawn on screen
%%%Entrance Point%%%
Draw.FillBox (240, 200, 422, 230, 0)
loop
    mousewhere (x, y, button)
    if button = 0 and whatdotcolour (x, y) = white then %button is not pressed and the mouse is over the entrance point
        locate (12, 32)
        put "[ALEX YOUNG OWNS YOU]" ..
        delay (100)
    elsif button = 0 and whatdotcolour (x, y) not= white then %button is not pressed and the mouse is not over the entrance point
        locate (12, 32)
        put "[CLICK HERE TO ENTER]" ..
        delay (100)
    elsif button = 1 and whatdotcolour (x, y) = white then %entering the store if button is pressed over the entrance point
    delay (1000)   
    cls
    exit
    end if
end loop
end intro
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Innitial Output
procedure innitialOutput
drawline (1,maxy-25,400,maxy-25,0) %horizontal line for item/price list
drawline (200,0,200,maxy,0) %vertical line for item/price list
Font.Draw("Item",70,maxy-20,shopFont,brightred)
Font.Draw("Price ($)",260,maxy-20,shopFont,brightred)
for i :1..5
locate (listOrder,1) %location of each consecutive item
put foodItems(i).. %.. so that the items do not cut off the vertical line for the item/price list
listOrder+=1 %each consecutive item is located 1 line below the previous
locate (listOrder-1,30)
put foodPrices (i)
end for
end innitialOutput
View.Set ("nooffscreenonly")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Changing Around The Shop
procedure changeShop
Font.Draw("Once In A Life Time Choice",maxx-320,110,shopFont,brightred)
drawbox(maxx-330,10,maxx-5,100,0)
locate (20,53)
put "Do you wish to:"..
locate (21,40)
put "1. Add an item / corresponding price?"..
locate (22,40)
put "2. Change a price?"..
locate (23,40)
put "3. Make a Purchase?"..
locate (24,40)
put "[Enter number of choice] "..
get answer

%Input For New Items / Corresponding Prices (OPTION #1)
if answer=1 %add an item / corresponding price option was chosen
then cls
put "How many items would you like to add? "..
get addItems
addPrices:=addItems
var addFoodItems:array 1..addItems of string
for a :1..addItems
put "Item # ",a, " to be added shall be named? "..
get addFoodItems(a):*
end for

%Input for New Prices
var addFoodPrices:array 1..addPrices of real
for b:1..addPrices
put "How much should item #", b, " cost? "..
get addFoodPrices(b)
end for

cls %clears the input section

%Output For New Items
listOrder:=3 %miniature variable reset
innitialOutput %procedure displaying the original items
for a:1..addItems
locate (listOrder,1) %situated beneath the preset items in the items column
put addFoodItems(a)..
listOrder+=1
end for

%Output for New Prices
listOrder:=3 %miniature variable reset
innitialOutput %procedure displaying the original items
for b:1..addPrices
locate (listOrder,30) %situated beneath the preset prices in the price column
put addFoodPrices(b):2:2..
listOrder+=1
end for
end if

%Changing The Price Of A Specific Item









end changeShop

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
innitialOutput
changeShop

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Apr 05, 2005 11:54 pm   Post subject: (No subject)

You should look into using records.

code:
type FoodItem : record
  name : string
  price : int
end record


Now you can have an array of FoodItems.

code:
items : array 1..5 of FoodItem


Getting an item's name:

code:
items (1).name


And the same item's price:

code:
items (1).price
lordofall




PostPosted: Tue Apr 05, 2005 11:56 pm   Post subject: (No subject)

well 1 way you coulda done it is to make the food an array of a record
the record would have 2 types
1-> the name of the food
2-> the price of the food
Now if you print numbers beside the food you can just ask the user to input the number of the food and then edit the price, otherwise when the user enters a string you will have to do a linear search to find the number of the item the user wishes to change, and then edit the price.

If you create a backup copy that is sorted alphabetically then you can use a binary search on that instead to find the number and then change the values on both arrays. (only useful if the list gets huge)

you can do this without records but after you find the number you just change foodprice(number).
jamonathin




PostPosted: Wed Apr 06, 2005 10:03 am   Post subject: (No subject)

Yeah and p.s. you can't hit 'Submit', then 'Stop', edit . . ., then 'Submit' again because it'll make 2 topics or 2 posts . . . i've done that a couple times Rolling Eyes
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 1  [ 4 Posts ]
Jump to:   


Style:  
Search: