program won't restart
Author |
Message |
blankout
|
Posted: Sat Apr 04, 2009 7:36 pm Post subject: program won't restart |
|
|
even with the procedure, i can't seem to get the program to restart after one cycle, please help
the program works fine, it's just that it doesn't repeat
Turing: | var name: string
var letters: int
var counter: int:= 0
put "What is your name?"
get name: *
cls
put "How many letters are there in ", name
get letters
cls
procedure restart
loop
put name (1..letters-counter )
delay (250)
cls
exit when counter=- 1
counter:=counter+ 1
restart
end loop
end restart
restart
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dusk Eagle
|
Posted: Sat Apr 04, 2009 8:22 pm Post subject: Re: program won't restart |
|
|
There's quite a few problems with your code. First of all, there is no need to be asking the user for the length of name. Instead, look up the length command. Second of all, your program is using global variables, which are a very bad thing. Read up on how to use parameters with your procedures/functions here. (Hint - name should be passed as a parameter, whereas counter and (if used) letters should be declared inside the procedure.)
Now take a look at the structure of your code. Does it ask for the user's name again? No, so you wouldn't expect it to repeat. But maybe you simply want the same name to "fade out" over and over again. Either way, look at your "exit when counter = -1" line. If we comment out the current put statement and add
we see that counter is continually increasing, not decreasing. So instead of exiting when the counter = -1, when would it make more sense to exit? I'll let you figure that part out yourself.
Now, once you have done all the above, to make the effect occur over and over again, simply add a loop around your "restart" line, and you'll witness the effect over and over again. |
|
|
|
|
|
blankout
|
Posted: Sat Apr 04, 2009 8:49 pm Post subject: Re: program won't restart |
|
|
ok well i tried to do what you said, and what i got is that the name still fades away, however i still can't repeat it, and i'm not entirely sure if i've gotten the exit when part correct
this is what i've managed to do
Turing: | var name: string
put "What is your name?"
get name: *
var counter: int:= 0
procedure restart
cls
loop
put name (1.. length (name )-counter )
put counter
delay (250)
cls
exit when counter= length (name )
counter:=counter+ 1
restart
end loop
end restart
restart
|
|
|
|
|
|
|
Dusk Eagle
|
Posted: Sat Apr 04, 2009 10:53 pm Post subject: Re: program won't restart |
|
|
You're very close to having your program working, only two things left to do. The first is to take out the line within your restart procedure that says nothing except "restart". What you have done is known as "recursion", which although not a bad thing, it is definitely not something you wanted to do here. The second thing you need to do is declare your counter variable inside your 'restart' procedure, rather than outside, like so:
Turing: |
procedure restart()
var counter : int := 0
%blah blah blah
end restart
|
The final thing to do is to loop calling the procedure, like so:
Turing: |
loop
restart
end loop
|
Do these three things and you'll have your program working. However, if you want to take a bonus step, look at the following code:
Turing: |
procedure restart (word: string)
var counter : int := 0
cls
loop
put word (1 .. length (word ) - counter )
delay (250)
cls
exit when counter = length (word )
counter := counter + 1
end loop
end restart
var name : string
put "What is your name?"
get name : *
loop
restart (name )
end loop
|
Notice the "procedure restart(word:string)" and the "restart(name)"? The restart(name) line "passes" the value of name into the procedure restart, which then stores a copy of that value known as word. One of the many advantages to doing it this way is demonstrated below:
Turing: |
var name1, name2 : string
get name1 :*
get name2 :*
loop
restart (name1)
restart (name2)
end loop
|
Notice how we could use more than one variable with restart()? This will prove to be immensely useful to you later on, and so is a good practice to start now. |
|
|
|
|
|
blankout
|
Posted: Mon Apr 06, 2009 2:56 pm Post subject: Re: program won't restart |
|
|
wow that fixed every problem i had thank you so much! |
|
|
|
|
|
|
|