Ending a program anywhere

This is a discussion on Ending a program anywhere within the C++ Programming forums, part of the General Programming Boards category; I saw this earlier post on another site( http://www.programmersheaven.com/c/M...ing=A9999F0006 ) and since I'm new to C++ (I've done BASIC), I ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Question Ending a program anywhere... how?

    I saw this earlier post on another site(http://www.programmersheaven.com/c/M...ing=A9999F0006)
    and since I'm new to C++ (I've done BASIC), I thought that a variation of it would be nice, since a lot of my programs are really annoying.

    How might I set it up so that no matter where in the program it is, if the keys "Alt+F4" are pressed, it will immediately end the program? Using the code at the abovementioned post, I could get "Q" to close at a particular point.

    I'm using Microsoft VC++ 6.0 as my compiler, and I am working on a WIN32 Console Application. Feel free to email me. (trauts@ebipm.net)
    Last edited by Trauts; 09-30-2002 at 04:33 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> I thought that a variation of it would be nice
    That piece of code is junk, pick better ones to learn from. Don't believe everything you read on a msg board.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    You could try keyb_event.
    The world is waiting. I must leave you now.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    How can I get it to constantly be checking for that so that no matter when they press it it does it? I was only using that program as an example.

    I could get just one key to end the program, but I'd have to have it use that every line... I don't know how to get it to do 2 things at once.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    At the risk of hanging myself out to dry here, what you're speaking of, I believe, is a "thread".

    My minimal understanding - which will become blatantly apparent to all - is that you're looking to write a piece of code that will be "suspended" during the normal execution, but "hang" in the background until some specific action is taken by the user, i.e. specific keystrokes.

    I have no knowledge of code that can be written in C/C++ that can accomplish what you want to do. This would seem, to me, to be an attribute of a specific compiler/development kit (Java) rather than of the standard C/C++ language.

    (Hammer was more concise, as usual. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    if u want it to check for that key an if statement would work would it not?

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    You want Windows hooks then.

    Perhaps you could try the windows board, or a search on Windows Hooks.
    The world is waiting. I must leave you now.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Sure, you could do that with multithreading, but I'll use Salems suggestion:

    ctrl+c, it will kill any console, (pretty sure).

    The upside is that the programmer need do nothing but tell the user that (if they don't already know it). The downside is that you won't have a chance to deallocate dynamic memory (unless you are using C++, in which case you can put cleanup code in the destructors of your classes, which *should* be called in that event).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    whoa... slow down people .. Poor old Trauts doesn't want to know about multi-threading apps just yet. The term walking before you run spring to mind.

    If you have it available, you can use kbhit() to determine if there's input to be read. If so, you can then use getch() to get that input. Extended keystrokes will require 2 calls to getch() to get all the information. This whole process requires your code to be running in a loop, checking kbhit() each time round.

    As Seb said though, CTRL+C is always a short way out if your desperate.

    From the original post:
    I thought that a variation of it would be nice, since a lot of my programs are really annoying.
    What's annoying about them? Maybe your design could do with a rework.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    yea bro, no matter what flaws your progs have, have pride because they are yours man : )

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Folks,

    Ctrl+Break - Lord knows I've used it often enough.

    ('Ctrl+c' copies highlighted text.)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>('Ctrl+c' copies highlighted text.)
    Maybe in the GUI environment... but at a console level its different.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Good point.

    Thanks.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I don't mean they're annoying, its just you can't easily exit them without a lot of trouble.

    On a completely different note, using '\a' to beep

    Code:
    cout << "Message\a\n";
    Is there a way to change the tone/pitch of the beep, so that you could play music as you can in QBasic? Or would I have to go into WAV, MIDI, etc. files to do it? I don't need to use \a to beep, thats just the only way I currently know.
    Last edited by Trauts; 09-30-2002 at 07:34 PM.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. help with ending program
    By Derek in forum C Programming
    Replies: 13
    Last Post: 06-25-2003, 12:10 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21