Thread: Getting user input without pausing

  1. #1
    Registered User
    Join Date
    May 2010

    Getting user input without pausing

    Is there a simple way to allow user input without having the program "wait" for it?

    For example, I want to write a function that counts down from 100 to 0. The only way to stop it would be if the user enters a certain phrase. I want to make sure that the count down is absolutely uninterrupted unless the exact phrase is entered.

    So, how would I have a variable ready to be changed by the user, and also a function running at "the same time" so to speak?


    Here's some mock code for what I can come up with so far.

    for (int i = 100; i > 0; i--)
      cin >> password;
      if (password != 'stop')
        // pause for 1 second
        cout << i << "..." << endl;
    So that's the basic idea of what I want, except in that example, the function will pause every time and "wait" for input right?
    Last edited by Xanderbeard; 07-10-2010 at 01:54 PM.

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    One way would require threads and thread synchronization.
    There are other solutions, but none of them are easy ways.
    Last edited by Elysia; 07-10-2010 at 02:40 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    It doesn't need threads. It just needs nonblocking input functions. Unfortunately there is no such thing in standard C++, but they certainly exist in other libraries. For example, Borland C++ used to have a function called kbhit() once upon a time, which would return true if there was data waiting to be read from the console and false otherwise.

    Dev-C++ may have an implementation of kbhit() (it has getche() ...), and if not the web describes a number of ways of doing it. Here's one reference for using kbhit() without ncurses on Linux: Non-blocking user input in loop without ncurses. – c/c++ programming by examples
    If you want to do this sort of thing seriously though you could look into ncurses (or pdcurses, which I hear is roughly equivalent but runs on Windows). Be warned that it might be quite a bit of work to learn a new API like ncurses, however.

    The short answer is that it's not easy, and if you're just trying to write a program that displays "the nuke will go off in X seconds!" unless you type the password, just for fun . . . it's probably not worth the hassle.

    [edit] More details:
    Last edited by dwks; 07-10-2010 at 02:43 PM.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. handling user input
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2009, 06:21 AM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 10:23 AM
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM