CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2005
    Posts
    33

    exit if a key is hit, else continue...

    I have a problem. I have a program that will run continuously. But I want to have an interupt to stop the program. How do I do it??

    I tried use getch but this will cause the program to pause at getch until a key is hit (which is not what I want). I want it to continue running until a key is hit before it stops.

    My program consist of a for loop which does the continuous running.

    I am using Borland C++ to compile and run my program.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: exit if a key is hit, else continue...

    Simply press Ctrl+C to abort your program. Use atexit to execute cleanup stuff.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: exit if a key is hit, else continue...

    Is this what you want?
    Code:
    while(!kbhit())
    {
      // do something
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    May 2005
    Posts
    33

    Re: exit if a key is hit, else continue...

    I only want a specific key that can stop the program. If I use kbhit(), it will allow any key hit to do that something rite?? How do I write so that it only allow a specific key, like ESC, to stop the program.

  5. #5
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Smile Re: exit if a key is hit, else continue...

    yeah, the Kbhit() is triggered when ever a character is hit. but since u are lookin for a specific charecter to be hit ....after the Kbhit() u can call getch() to retrive the char that has been waitin ...n then compare it with the required hit that u want..( Esc )....before u give call to the Exit() .
    hope this should help u.

    cheers :-)
    sreehari

  6. #6
    Join Date
    May 2005
    Posts
    33

    Re: exit if a key is hit, else continue...

    but if i user getch(), it will pause there until a key is hit (this is not what i want).

    I want my program (which consist of a for loop) to continue running until ESC is hit. When ESC is hit, the program will stop.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: exit if a key is hit, else continue...

    Quote Originally Posted by tlp0605
    but if i user getch(), it will pause there until a key is hit (this is not what i want).

    I want my program (which consist of a for loop) to continue running until ESC is hit. When ESC is hit, the program will stop.
    No, because KbHit() has just told you that there is a key hit waiting, so there's no pause.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: exit if a key is hit, else continue...

    On each iteration in your for loop call _kbhit() to see if a key was pressed. This function will not wait. If a key is pressed it will return non-zero. If that's the case use getch to get the key that was pressed and if it's esc, abort execution.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  9. #9
    Join Date
    May 2005
    Posts
    33

    Re: exit if a key is hit, else continue...

    u mean if i use kbhit 1st then getch to check what key is hit, it will not pause??

  10. #10
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: exit if a key is hit, else continue...

    Quote Originally Posted by tlp0605
    u mean if i use kbhit 1st then getch to check what key is hit, it will not pause??
    Yes, something like:
    Code:
    if (_kbhit())
    {
        // use getch to check key
    }
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  11. #11
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: exit if a key is hit, else continue...

    The program wil not pause executing as the scope of the Getch() vil only be inside the If( Kbhit()) block....so the Getch() vil only be called if any char has been hit by the user

  12. #12
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: exit if a key is hit, else continue...

    Quote Originally Posted by tlp0605
    u mean if i use kbhit 1st then getch to check what key is hit, it will not pause??
    Exactly because the char is already in the input buffer so getch() will receive it from there.

    BTW Marc, with the Borland C++ compiler the function is kbhit() and not _kbhit().
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  13. #13
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: exit if a key is hit, else continue...

    Quote Originally Posted by cilu
    BTW Marc, with the Borland C++ compiler the function is kbhit() and not _kbhit().
    Sorry, my mistake
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured