Like Tree2Likes
  • 1 Post By oogabooga
  • 1 Post By std10093

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

This is a discussion on Is there a way to make program respond to certain input at any point of the program? within the C++ Programming forums, part of the General Programming Boards category; I have an exercise, where I have to have a possibility to initiate a mass rabbit cull(program itself basically produces ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    }
    Vana Papi likes this.
    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,681
    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.
    Vana Papi likes this.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    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%.

  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,307
    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%.

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, 06: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, 04:33 PM
  5. Replies: 2
    Last Post: 03-25-2002, 04:49 AM

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