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

Username:   Password: 
 RegisterRegister   
 Trouble with variables
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
asdfasdf123




PostPosted: Thu Dec 11, 2014 1:49 pm   Post subject: Trouble with variables

What is it you are trying to achieve?
I'm trying to make a slot machine game.


What is the problem you are having?
I can't use certain variables outside of a for loop, but I need them to make the game work.


Describe what you have tried to solve this problem
I've tried copy-pasting the rest of the program that uses those variables inside the for loop, but it didn't work.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Here's the part of the code I'm having trouble with:

Turing:


for i : 1 .. 100
                delay (50)
                drawfillbox (CENTREX - 150, CENTREY - 70, CENTREX - 60, CENTREY + 70, white)
                drawfillbox (CENTREX + 150, CENTREY - 70, CENTREX + 60, CENTREY + 70, white)
                drawfillbox (CENTREX - 40, CENTREY - 70, CENTREX + 40, CENTREY + 70, white)
                var left := Rand.Int (1, 100)
                var middle := Rand.Int (1, 100)
                var right := Rand.Int (1, 100)
                if left < 50 then
                    include "BarLeftMiddle.t"
                elsif left < 80 then
                    include "LemonLeftMiddle.t"
                elsif left < 99 then
                    include "PlumLeftMiddle.t"
                elsif left = 100 then
                    include "CherryLeftMiddle.t"
                end if
                if middle < 50 then
                    include "LemonMiddleMiddle.t"
                elsif middle < 80 then
                    include "PlumMiddleMiddle.t"
                elsif middle < 99 then
                    include "BarMiddleMiddle.t"
                elsif middle = 100 then
                    include "CherryMiddleMiddle.t"
                end if
                if right < 50 then
                    include "PlumRightMiddle.t"
                elsif right < 80 then
                    include "BarRightMiddle.t"
                elsif right < 99 then
                    include "LemonRightMiddle.t"
                elsif right = 100 then
                    include "CherryRightMiddle.t"
                end if
                if i = 100 then
                    var left2 : int := left
                    var middle2 : int := middle
                    var right2 : int := right
                end if
            end for
            put left2


The "put left2" is just there to see if it works. Without it, the code runs normally, but with it, it says that "left2 has not been declared". I added left2, middle2, and right2 after seeing that left, right, and middle didn't work. I have previously declared CENTREX and CENTREY as maxx div 2 and maxy div 2 respectively.

Please specify what version of Turing you are using
4.1 version 1.0.1
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Dec 11, 2014 2:08 pm   Post subject: RE:Trouble with variables

It looks like you haven't learned about variable scope yet. The basic idea is that variables are only defined for some parts of the program, not for the program as a whole.

For example, this doesn't work:
code:

for i : 1 .. 2
    var a : int := i
end for
put a    % "a has not been declared"


But this does work:
code:

var a : int
for i : 1 .. 2
    a := i
end for
put a   % outputs "2"


You might have noticed that this actually applies to the for-loop counter variable too:
code:

for i : 1 .. 2
    put i   % works fine
end for
put i   % "i has not been declared"


This also works for other "blocks", like if blocks:
code:

var a : int := 0
if ( condition ) then
    a := 1
    var b : int := 2
end if
put a    % puts either 0 or 1, depending on (condition)
put b    % "b has not been declared"


This is deliberate, because it lets the programmer choose where variables are accessible, which makes it easier to write clean, organized code. This feature also makes it easier to understand a program a little bit at a time, by understanding which information can "leave" a loop-end loop or if-end if.

You could consider variables declared inside a block (for-loop, if, etc) to be like "temporary variables" that disappear when you are done the loop. When you get to learning about procedures and functions, the use of these temporary variables will be even more important.

So to solve your current problem, just make sure that you declare your variables outside the loop. You can still assign them inside the loop, you just have to declare them outside the loop.
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  [ 2 Posts ]
Jump to:   


Style:  
Search: