
-----------------------------------
nonamedude
Thu May 28, 2009 7:14 pm

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)




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

-----------------------------------
Dusk Eagle
Thu May 28, 2009 7:58 pm

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  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 . 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: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:

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?

times := times + 1
    get view (times)
    exit when view (times) = 1 or view (times) > 1


-----------------------------------
Dusk Eagle
Thu May 28, 2009 7:58 pm

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  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 . 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: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:

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?

times := times + 1
    get view (times)
    exit when view (times) = 1 or view (times) > 1


-----------------------------------
nonamedude
Thu May 28, 2009 8:12 pm

Re: Procedure problem
-----------------------------------
ok lots of questions....
gimme a sec to look things through

-----------------------------------
nonamedude
Thu May 28, 2009 8:20 pm

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