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

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




PostPosted: Mon May 19, 2008 3:16 pm   Post subject: exiting

How would I go about making it so a user can press one key at anytime during a program, and the program will close?
I tried doing something along these lines, but it didn't really work out:

code:

loop
if var = "n" then
exit
end loop


All that did was wait for me to press "n" then it would go onto the rest of my program...

Any help would be appreciated!

Thanks
-Yasser
Sponsor
Sponsor
Sponsor
sponsor
jinjin




PostPosted: Mon May 19, 2008 3:26 pm   Post subject: Re: exiting

You'd have to use the Input.Keydown function for that.

Check Input.KeyDown in the Turing help file for more info.

Here is some pseudo-code to start you off:

var (something) : array char of boolean
loop
exit when key input is (something)
end loop
YasserSalama




PostPosted: Mon May 19, 2008 3:26 pm   Post subject: RE:exiting

Okay, thanks for your help. I'll try and look it up and figure it out.
YasserSalama




PostPosted: Mon May 19, 2008 3:39 pm   Post subject: RE:exiting

I'm having some trouble figuring out the actual command. I have something like this:

code:

loop
        var chars : array char of boolean
        Input.KeyDown (chars)
        if chars ('t') then
            exit
        end if
%PROGRAM
end loop


This doesn't seem to work. I'm not sure how I would go about doing it jinjin's way either. The command just seems confusing to me.
Sean




PostPosted: Mon May 19, 2008 3:51 pm   Post subject: Re: exiting

Turing:

var answer : string

loop
     %Program
     put "Would you like to exit? (Y/N)"
     get answer

     if answer = "Y" or answer = "y" then
          exit
     end if
end loop


You need your program to be looping before hand, and doing what it needs, then have your exit at the end of the program, or where ever you wish to exit it from.
YasserSalama




PostPosted: Mon May 19, 2008 3:55 pm   Post subject: RE:exiting

I appreciate your help Sean, but I was looking for something where it wasn't a question, but anytime during the program they can just press a key that is already pre-determined by me and it will exit.

So at the beginning of the program, I will say,
"If at anytime you would like to quit the program, press N." This would make it so during the program if they want to exit, they can just press n and it will exit without having to ask a question.

It might sound confusing because I can't explain that well, but I hope it clears somethings up!
richcash




PostPosted: Mon May 19, 2008 4:37 pm   Post subject: Re: exiting

What is wrong with your last code snippet, it should work? Did you get an error?

If the rest of your program has a loop in it then that might be a reason it's not working. Post if that is the case.
YasserSalama




PostPosted: Mon May 19, 2008 4:57 pm   Post subject: RE:exiting

Yea my program is pretty long. It has for loops in it and getch commands for any button on the keyboard. That might be the problem, but I don't know how to get around it.
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Mon May 19, 2008 5:16 pm   Post subject: Re: exiting

You have to check for the key being pressed in every loop. Make a new boolean variable set to false outside any loops. If in any of your loops the certain key is pressed, then set this boolean variable to true. Now, at the end of every loop in your program put exit when boolean_variable_name.

Try to implement that and see if it works.
andrew.




PostPosted: Mon May 19, 2008 5:18 pm   Post subject: RE:exiting

How about this.

Turing:
var window : int
window := Window.Open ("graphics:max,max")

var chars : array char of boolean

loop
    %PROGRAM
    Input.KeyDown (chars)
    if chars (KEY_ALT) and chars (KEY_F4) then
        Window.Close (window)
        exit
    end if
end loop


So I open a window. And when I press ALT and F4, the window closes and the program exits.

I posted this before seeing rickcash's post. This is what he means.

Turing:

var closeprogram : boolean := false

loop
    %PROGRAM
    Input.KeyDown (chars)
    if chars (KEY_ALT) and chars (KEY_F4) then
        Window.Close (window)
        exit
closeprogram := true
    end if
end loop

if closeprogram=false then
    loop
    %MORE PROGRAM
        Input.KeyDown (chars)
        if chars (KEY_ALT) and chars (KEY_F4) then
            Window.Close (window)
            exit
        end if
    end loop
end if
gitoxa




PostPosted: Mon May 19, 2008 9:02 pm   Post subject: RE:exiting

You guys should be using an 'exit when <cond>' rather than an if, with an exit inside. It's better coding conventions, and easier to read.
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  [ 11 Posts ]
Jump to:   


Style:  
Search: