Thread: Is there a way to make program respond to certain input at any point of the program?

  1. #1
    Registered User Vana Papi's Avatar
    Join Date
    Jul 2012
    Posts
    11

    Is there a way to make program respond to certain input at any point of the program?

    I have an exercise, where I have to have a possibility to initiate a mass rabbit cull(program itself basically produces structs of bunny information).
    ★★ Allow the user to hit the 'k' key to initiate a mass rabit cull! which causes half of all the rabits to be killed (randomly chosen).
    It was long time ago, but I remember that AutoIt had had functions to respond to user input at any point of the program, but I couldn't find one for c++, are there any?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Not totally sure..(you have to explain better);
    but you're probably thinking about Asynchronous Input handling, which I'm not sure if possible with standard C++.
    (I tried with threads and it turned messy... probably due to my inexperience with concurrent programming.
    What I did was, checking in a main loop to see if a console input thread has completed, and doing the necessary stuff and relaunching the thread, if so.)

    Or you could take the easy way and just use line buffered standard blocking input, if you can structure it in that way.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Code:
    while (...)
    {
        if (_kbhit())
        {
            switch (_getch())
            {
            case 'k': // cull rabbits
                break;
            }
        }
    
        // other stuff
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Vana Papi View Post
    I have an exercise, where I have to have a possibility to initiate a mass rabbit cull(program itself basically produces structs of bunny information).

    It was long time ago, but I remember that AutoIt had had functions to respond to user input at any point of the program, but I couldn't find one for c++, are there any?
    I believe that you should have a main loop,and at the start or at the end of every loop you if user has typed something or not.If user inputs 'k' then the salvation of the bunnies may begin :P There is a function that is designed for this purpose,it checks if user has given some input or not..However i do not remember the function's name...

    EDIT->I had not seen oogabooga's post because i was typing mine(and searching...).He knows the name of the function i stated above,the name is : _kbhit() and it returns something non-zero if user inputs something.If user does not input something _kbhit() returns zero .
    Last edited by std10093; 07-14-2012 at 08:06 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The functions described by oogabooga are specific to particular compilers.

    In standard C++, it is not possible. It is necessary to read documentation for your host system (windows, linux, etc) and also your particular compiler and library, to see if they support such functions. The names of such functions and associated header files, if they exist for a particular implementation, are often specific to that implementation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User Vana Papi's Avatar
    Join Date
    Jul 2012
    Posts
    11
    Quote Originally Posted by manasij7479
    Not totally sure..(you have to explain better);
    but you're probably thinking about Asynchronous Input handling, which I'm not sure if possible with standard C++.
    (I tried with threads and it turned messy... probably due to my inexperience with concurrent programming.
    What I did was, checking in a main loop to see if a console input thread has completed, and doing the necessary stuff and relaunching the thread, if so.)
    Or you could take the easy way and just use line buffered standard blocking input, if you can structure it in that way.
    I meant a hotkey, like HotKeySet in AutoIt

    Quote Originally Posted by grumpy View Post
    The functions described by oogabooga are specific to particular compilers.

    In standard C++, it is not possible. It is necessary to read documentation for your host system (windows, linux, etc) and also your particular compiler and library, to see if they support such functions. The names of such functions and associated header files, if they exist for a particular implementation, are often specific to that implementation.
    But MS Visual Studio has the most potential of having those non-standard functions and librarys?(In case I need to use non-standard in future)



    Seems like I have to check if key is pressed by myself, I tought there would be a function for it.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Virtually all compilers have non-standard functions and libraries. MS Visual Studio (or the compiler and library with it) is no different.

    If you want your program to be interrupted when a key is pressed, you need to set that up yourself. The "how" of that depends on requirements of your program (in particular, what happens to other functions in your program if they get interrupted mid-way).

    Usually the simplest and most commonly used technique is to periodically check for keyboard hit, and cease other activities (eg repetitive calculations) - which is essentially what oggabooga showed you. Without knowing more about your program (for example, is it multi-threaded and, if so, what data is shared between threads? are functions reentrant?) it is not possible to give advice on other techniques. Those other techniques cause lots of problems if not used properly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a program that will make the number i input to words
    By janjan in forum C++ Programming
    Replies: 4
    Last Post: 02-14-2011, 07:16 PM
  2. Replies: 11
    Last Post: 06-27-2008, 03:39 PM
  3. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  4. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM
  5. Replies: 2
    Last Post: 03-25-2002, 05:49 AM