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

Username:   Password: 
 RegisterRegister   
 the "Getch" procedure
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rcmp1234




PostPosted: Thu May 01, 2008 8:19 pm   Post subject: the "Getch" procedure

Turing:

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!
Sponsor
Sponsor
Sponsor
sponsor
The_Bean




PostPosted: Thu May 01, 2008 8:37 pm   Post subject: Re: the "Getch" procedure

Turing:

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




PostPosted: Fri May 02, 2008 4:06 pm   Post subject: Re: the "Getch" procedure

you could even use The_Bean's method to have the procedure call itself if they don't enter "e"
Nick




PostPosted: Fri May 02, 2008 4:08 pm   Post subject: RE:the "Getch" procedure

or a loop inside the procedure
Turing:
procedure inputletter
loop
    getch (reply)
    if reply = "e" then
    menu
    return
    end if
end loop
end inputletter
Tony




PostPosted: Fri May 02, 2008 4:20 pm   Post subject: RE:the "Getch" procedure

or call procedure after the loop
Turing:

procedure inputletter
    loop
       getch (reply)
       exit when reply = "e"
    end loop
    menu
end inputletter
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
btiffin




PostPosted: Fri May 02, 2008 5:16 pm   Post subject: Re: the "Getch" 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




PostPosted: Fri May 02, 2008 5:38 pm   Post subject: RE:the "Getch" procedure

Thanks everyone...that did the trick!
btiffin




PostPosted: Fri May 02, 2008 5:39 pm   Post subject: Re: the "Getch" procedure

My apologies to Tony. Smile His code may well be the "converting this to Turing will be left as exercise" part. Excuse my lack of Turing syntax details.

Cheers
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: