Thread: How do you read input from the user without waiting?

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    11

    How do you read input from the user without waiting?

    Hi,
    I've tried getchar, but it freezes everything that's happening until the user enters input. I want to know how to loop something while the user enters input and then have the program react to the input. Any help would be appreciated. Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    There is no portable way to do this. You need find the way your OS supports non-blocking input or Asynchronous I/O.

    Tim S.
    Last edited by stahta01; 01-23-2012 at 03:31 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    You can use a library like ncurses (pdcurses on Windows) to get non-blocking input. There *are* other options (namely, using functionality from your OS or putting IO into a separate thread) but they are either less portable or more complicated, respectively, than the ncurses route.
    Consider this post signed

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Thanks, I'll try that.
    Edit: unfortunately, my compiler (Dev) doesn't have pdcurses or ncurses. how would I get these files?
    Last edited by Isaiah; 01-23-2012 at 03:40 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Isaiah View Post
    Thanks, I'll try that.
    Edit: unfortunately, my compiler (Dev) doesn't have pdcurses or ncurses. how would I get these files?
    Public Domain Curses | Free Development software downloads at SourceForge.net

    You might do the non-portable way of getche, I have never used it so it might not be the right conio function.
    getch() equivalent for Dev C++

    Tim S.
    Last edited by stahta01; 01-23-2012 at 03:49 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, scanf isn't waiting for user input
    By Blasz in forum C Programming
    Replies: 5
    Last Post: 05-07-2010, 05:32 AM
  2. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  3. Read a 4x4 matrix from user input
    By JHaney in forum C++ Programming
    Replies: 9
    Last Post: 11-29-2005, 07:56 PM
  4. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  5. Waiting for input
    By Jareds411 in forum Windows Programming
    Replies: 5
    Last Post: 09-16-2004, 06:18 PM