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

Username:   Password: 
 RegisterRegister   
 Procedure problem
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
nonamedude




PostPosted: Thu May 28, 2009 7:14 pm   Post subject: Procedure problem

What is it you are trying to achieve?
A store program


What is the problem you are having?
The program works fine but if the user reapeats it it stops because of the procedures, (i cant use one thats not global to the rest)

Describe what you have tried to solve this problem
Do the whole program as one procedure but it just meeses it up

NOTE: I can only use limited code to this this program(its not like i dont want to it just that i want to do it using what i studied)
loops
for loops
procedures
arrays
if statements (boolean values)
counters
and thats about it (and the basics of course)


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


var game1 : string
var game2 : string
var game3 : string
var view : array 1 .. 1000 of int
var answer : array 1 .. 1000 of string
var quantity : array 1 .. 1000 of int
var response : string
var back : string
var name : array 1 .. 1000 of string
name (1) := "Game 1"
name (2) := "Game 2"
name (3) := "Game 3"
var product : array 1 .. 1000 of real
product (1) := 29.99
product (2) := 30.99
product (3) := 19.99
var times : int

procedure productsondisplay
    cls
    loop
        times := 0
        put ""
        put "We have items on display today, please have a look at it"
        put "Game1"
        put "Game2"
        put "Game3"
        put "To view a game you are interested in buying please enter the game's No"
        times := times + 1
        get view (times)
        exit when view (times) = 1 or view (times) > 1
        cls

    end loop

end productsondisplay

procedure buy


    put "How many would you like buy?" ..
    get quantity (1)


end buy

procedure buy1
    loop
        put "Would you like to go back to the products"
        get back
        if back = "yes" or back = "Yes" then
            productsondisplay
        else
            if back = "no" or back = "No" then
                put "Please enter bill to show your purchases"
                get response
                if response = "bill" or response = "Bill" then
                    put "This is your bill"


                end if
            end if
        end if

        exit when back = "yes" or back = "Yes" or back = "no" or back = "No"

    end loop
end buy1



procedure gamea
    put "picture"
    put ""
    put ""
    put ""
    put ""
    put ""
    put "Requirements"
    put "price"

    put "How many would you like buy?" ..
    get quantity (1)



    buy1
    cls
end gamea

procedure gameb
    put "picture"
    put ""
    put ""
    put ""
    put ""
    put ""
    put "Requirements"
    put "price"

    put "How many would you like buy?" ..
    get quantity (2)



    buy1
    cls
end gameb

procedure gamec
    put "picture"
    put ""
    put ""
    put ""
    put ""
    put ""
    put "Requirements"
    put "price"


    put "How many would you like buy?" ..
    get quantity (3)



    buy1
    cls
end gamec

procedure games
    if view (1) = 1 then
        put "picture"
        put ""
        put ""
        put ""
        put ""
        put ""
        put "Requirements"
        put "price"

        put "How many would you like buy?" ..
        get quantity (1)



        buy1
        cls
    else
        if view (1) = 2 then
            put "picture"
            put ""
            put ""
            put ""
            put ""
            put ""
            put "Requirements"
            put "price"

            put "How many would you like buy?" ..
            get quantity (2)



            buy1
            cls
        else
            if view (1) = 3 then
                put "picture"
                put ""
                put ""
                put ""
                put ""
                put ""
                put "Requirements"
                put "price"


                put "How many would you like buy?" ..
                get quantity (3)



                buy1
                cls
            end if
        end if
    end if

end games

procedure bill
    if view (times) = 1 then
        put
            name (1), product (1), quantity (1)

    else
        if view (times) = 2 then

            put
                name (2), product (2), quantity (2)

        else
            if view (times) = 3 then
                put
                    name (3), product (3), quantity (3)
            end if
        end if
    end if
end bill

productsondisplay
games
bill




Please specify what version of Turing you are using
4.1.1.0
Sponsor
Sponsor
Sponsor
sponsor
Dusk Eagle




PostPosted: Thu May 28, 2009 7:58 pm   Post subject: Re: Procedure problem

First of all, your arrays should not be going from 1 to 1000. This is a terrible programming practice for many reasons. You should either make them contain only as many elements as you need, or you should use flexible arrays if you are not sure how many elements you will need.

Secondly, you are using procedures wrong (but good job for using them in the first place!). Procedures should not be modifying the value of variables that are declared outside their scope (i.e. outside of themselves). You should probably look into functions and parameters here. Note that any variables a procedure relies upon that are not passed in as parameters should be declared inside the procedure itself, and not outside.

As for the problem with your code, it lies right here:
Turing:
put "Would you like to go back to the products"
    get back
    if back = "yes" or back = "Yes" then
        productsondisplay
When this condition occurs, it will properly go back to productsondisplay, but then have no where to go. What you should do instead is:
Turing:

loop
    productsondisplay
    games
    bill
    put "Buy again?"
    get answer %or whatever variable you use
    exit when answer ~= "yes"
end loop

One more thing: What do you think the following piece of code is supposed to do, anyway? Why are you setting time to zero every time only to increment it by one? Why does "view" have 1000 elements when you only use the first? Why do you have a loop that will always exit the first time through, assuming the user entered a positive number?
Turing:

times := times + 1
    get view (times)
    exit when view (times) = 1 or view (times) > 1
Dusk Eagle




PostPosted: Thu May 28, 2009 7:58 pm   Post subject: Re: Procedure problem

First of all, your arrays should not be going from 1 to 1000. This is a terrible programming practice for many reasons. You should either make them contain only as many elements as you need, or you should use flexible arrays if you are not sure how many elements you will need.

Secondly, you are using procedures wrong (but good job for using them in the first place!). Procedures should not be modifying the value of variables that are declared outside their scope (i.e. outside of themselves). You should probably look into functions and parameters here. Note that any variables a procedure relies upon that are not passed in as parameters should be declared inside the procedure itself, and not outside.

As for the problem with your code, it lies right here:
Turing:
put "Would you like to go back to the products"
    get back
    if back = "yes" or back = "Yes" then
        productsondisplay
When this condition occurs, it will properly go back to productsondisplay, but then have no where to go. What you should do instead is:
Turing:

loop
    productsondisplay
    games
    bill
    put "Buy again?"
    get answer %or whatever variable you use
    exit when answer ~= "yes"
end loop

One more thing: What do you think the following piece of code is supposed to do, anyway? Why are you setting time to zero every time only to increment it by one? Why does "view" have 1000 elements when you only use the first? Why do you have a loop that will always exit the first time through, assuming the user entered a positive number?
Turing:

times := times + 1
    get view (times)
    exit when view (times) = 1 or view (times) > 1
nonamedude




PostPosted: Thu May 28, 2009 8:12 pm   Post subject: Re: Procedure problem

ok lots of questions....
gimme a sec to look things through
nonamedude




PostPosted: Thu May 28, 2009 8:20 pm   Post subject: Re: Procedure problem

ok i dont want to set my time to 0 i want it to take time every time and add one to it.< every time it loops

I tried what you said but oen problem it only displays the product that i bought, i need it to display everything...
and i stote 1..1000 because i am uncertain of the amount<------ please tell me any disavantages of this vs storing it to the number of elements you need

I took a look at the walkthorugh and i'd love to use those codes but i as i said before it is not that i dont want to its just i need to do it with what i learnt so far
nonamedude




PostPosted: Thu May 28, 2009 9:32 pm   Post subject: Re: Procedure problem

If there are guys who answer i might nt be able to answer for a few hours because i wont be online
nonamedude




PostPosted: Fri May 29, 2009 12:27 pm   Post subject: Re: Procedure problem

Anyone there?
jbking




PostPosted: Fri May 29, 2009 1:17 pm   Post subject: Re: Procedure problem

nonamedude @ Thu May 28, 2009 6:20 pm wrote:
I tried what you said but oen problem it only displays the product that i bought, i need it to display everything...


How is the procedure supposed to know all the other stuff it is supposed to show? This is where a parameter can be useful I think.
Sponsor
Sponsor
Sponsor
sponsor
nonamedude




PostPosted: Fri May 29, 2009 7:23 pm   Post subject: Re: Procedure problem

ok i'll try and read the parameters walkthrough but if there is a way of doing this wihtout using parameters please tell me cause i was supposed to be able to do this wthout any additional code (to the one i mentioned above)
nonamedude




PostPosted: Sat May 30, 2009 4:51 pm   Post subject: Re: Procedure problem

Is there a way to make this work cause i am seriously stuck, i got a hint that if i use for loops and store the info in arrays it would make this much easier but i cant seem to get it right Confused . An example would be great
tjmoore1993




PostPosted: Sat May 30, 2009 5:08 pm   Post subject: RE:Procedure problem

Turing:
procedure BLAH (number :int)
end BLAH

%calling it

BLAH (2390349)


Use a procedure like that can help a lot because you have too many of them for such a simple program. Looping it would also work.
nonamedude




PostPosted: Sat May 30, 2009 5:14 pm   Post subject: Re: Procedure problem

ok..... so hw would that help me with the bill<----- thats my main problem
i need to display everthing the customer bought but in the end i only display what he recently bought
tjmoore1993




PostPosted: Sat May 30, 2009 6:10 pm   Post subject: RE:Procedure problem

Turing:
var Selection, Name : string
var SelectionOK, stream : int
var ItemExists : boolean := false
var Price : real

procedure Product (Item : int)
    loop
        if File.Exists ("Data/Item." + intstr (Item) + ".data") then
            open : stream, "Data/Item." + intstr (Item) + ".data", get
            get : stream, Name
            get : stream, Price
            close : stream
            ItemExists := true
            exit
        else
            ItemExists := false
            put "The item you are trying to buy does not exist."
            delay (1000)
            exit
        end if
    end loop
end Product

loop
    loop
        cls
        put "Welcome to my shop, what would you like to buy?"
        % Have selection right here
        loop
            get Selection
            if strintok (Selection) then
                SelectionOK := strint (Selection)
                exit
            else
            end if
        end loop
        Product (SelectionOK)
        if ItemExists = true then
            exit
        end if
    end loop
    put "Name : ", Name
    put "Price : $", Price
    % action HERE
    delay (20000000) % remove later...
end loop


Sample of what could be a better approach.

Edit*
Flexible arrays will help you when you are figuring out which items you bought by creating a new array each time you bought something. Why not array? Think of it this way... If someone bought 15 items or more you'd need to define a large array and it still might not be enough. Flexible array allows you to change the array allowing maximum efficiency.

Figure out your program first before you do so, maybe ask questions about it as well.
nonamedude




PostPosted: Sun May 31, 2009 2:13 pm   Post subject: Re: Procedure problem

Ok the program works fine but i dont understand any of the codes, it is nt that i want do not want to learn them but i have to be able to do this assignment withought using codes tthat i did nt study
i gt
arrays <--- hw to store them in arrays
procedure<------ basic procedure
counters
loops, for loop
if staments

i would gradly aprreciate it if you gave me an example using these, in the meantime i'll work on the one you gave me
tjmoore1993




PostPosted: Sun May 31, 2009 5:30 pm   Post subject: RE:Procedure problem

Turing:


procedure Action (WhichAction : int)
if WhichAction = 1 then
put 1+2
elsif WhichAction = 2 then
put 1000 + 337
end if
end Action

Action (1)

% or

procedure Action (WhichAction : string)
if WhichAction = "FLY" then
put 1+2
elsif WhichAction = "JUMP" then
put 1000 + 337
end if
end Action

Action (JUMP)



I do not know how to explain this as good but guides do help and give great descriptive sentences on this stuff. Also, about the I/O portion of the code stream is basically another word for moving data (in and out). When you want to retrieve data you can use a 'read' or 'get' statement or vice versa 'write' or 'put'.

Boolean is another way of saying 'true' or 'false'.

Also notice how in the procedure when I give it a number it loads that file thus allowing ultimate efficiency because you do not need to load things you will probably not look at but load things when you want to see them.
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  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: