
-----------------------------------
rcmp1234
Thu May 01, 2008 8:19 pm

the &quot;Getch&quot; procedure
-----------------------------------
 
procedure inputletter
    getch (reply)
    if reply = "e" then
    menu
    end if
    end inputletter
 
*Note that menu is a procedure and has been declared before*

Here I have a part of my program that asks the user to press a key. If the user presses e then he will be taken back to the menu. The problem is if he presses another letter the program will still keep on executing. Is there any way I can have the program wait for the user to press e (and does not continue executing if he presses another key)? 

I was thinking of putting it in a loop but you cant put a procedure in a loop

ANd yes, I am a newbie in Turing
Thanks!

-----------------------------------
The_Bean
Thu May 01, 2008 8:37 pm

Re: the &quot;Getch&quot; procedure
-----------------------------------

procedure inputletter 
    getch (reply) 
    if reply = "e" then 
        menu 
    else
        doSomethingElse
    end if 
end inputletter 


I just added in the "else" which means if it reply= anything but "e" then it does something else(what ever you might want it to be).
But if you want the program to totally terminate if they put anything but "e", then work exit, return, or quit in there some how (depending on what the rest of your program is doing).

-----------------------------------
TheGuardian001
Fri May 02, 2008 4:06 pm

Re: the &quot;Getch&quot; procedure
-----------------------------------
you could even use The_Bean's method to have the procedure call itself if they don't enter "e"

-----------------------------------
Nick
Fri May 02, 2008 4:08 pm

RE:the &quot;Getch&quot; procedure
-----------------------------------
or a loop inside the procedure 
procedure inputletter
loop
    getch (reply)
    if reply = "e" then
    menu
    return
    end if
end loop
end inputletter 

-----------------------------------
Tony
Fri May 02, 2008 4:20 pm

RE:the &quot;Getch&quot; procedure
-----------------------------------
or call procedure after the loop
 
procedure inputletter
    loop
       getch (reply)
       exit when reply = "e"
    end loop
    menu
end inputletter


-----------------------------------
btiffin
Fri May 02, 2008 5:16 pm

Re: the &quot;Getch&quot; procedure
-----------------------------------
These are all good answers so far.

Do a google on Structured Programming for more info, but basically you want to use a

DO
...
UNTIL

structure for that type of problem.

DO
get a key
UNTIL key equals "e"

Converting that to Turing will be left as an exercise.

In purist terms, a
LOOP
  IF ... RETURN  ENDIF
END LOOP   is defined as a "hidden goto";  not horribly bad form, and quite acceptable now-a-days, but a DO ... UNTIL  is just that little bit more structured, and usually more efficient, as there are no magical internal stack winds and unwinds or jumps going on behind the scenes.

Cheers

-----------------------------------
rcmp1234
Fri May 02, 2008 5:38 pm

RE:the &quot;Getch&quot; procedure
-----------------------------------
Thanks everyone...that did the trick!

-----------------------------------
btiffin
Fri May 02, 2008 5:39 pm

Re: the &quot;Getch&quot; procedure
-----------------------------------
My apologies to Tony.  :)  His code may well be the "converting this to Turing will be left as exercise" part.  Excuse my lack of Turing syntax details.

Cheers
