Thread: How to stop reading input...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    How to stop reading input...

    In my program, i have a timer after which a few inputs. Now the problem is that when the timer function is in process, whatever i type goes as my input. I want to avoid that.

    I tried fflush( stdin ); but it didn't work.
    So i just created an array and used
    for( i=0; ; i++ )
    h[i] = getchar();


    now i can give a condition h[i]!=EOF or something, but the thing is for tht i need to tell the user to input EOF which i don't want to...

    All i need to do is block the input, call the timer function, and resume the input back.. If blocking is not possible is there a way by which i can stop taking an input? The loop above goes on till the buffer gets cleared and then waits for the next input and continues till i give EOF (if i put that as the condition).. i can't have a counter controlled loop because i dunno how much the user enters... i can't have sentinel controlled because the user need not enter the value..
    is there a way?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Now, I'm officially confused!

    You said the input is after the timer, but now the timer is in process while the user is entering data?
    Why can't a newline end input?

    Maybe you could post a small example of the problem? No offense, but you are no writer.

    fflush(stdin) doesn't work on input streams, only on output streams. (It's like trying to flush something down the kitchen faucet, instead of the drain).

    Welcome to the forum, Intull!

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is actually no standard way to do this. You will need to use some non-standard way of checking if there is input and, if it is input there, read in and discard what is there.

    There are standard techniques if you KNOW there will be unwanted input in the stream, but no standard techniques that are guaranteed to work if there MIGHT be input left in the stream.

    That's not actually a drama in your case: you're using timer functions and they are not standard either. So, you might as well find a non-standard means of flushing the input stream that works on your target machine.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    for example

    i take an integer after a timer... but wen the timer is in process i can type anything.. and that goes as my input. that shouldn't happen.
    and because of this newline can't end my data...
    Last edited by intull; 02-19-2011 at 10:41 PM.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    is there a way by which i can know if there are stuff in the buffer?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try this...

    Code:
    // do timer here
    
    while (getchar() != EOF);
    
    // get input here
    The loop should clear out your input buffer for you before you ask your user for keyboard input.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    but in that case, i need to manually enter EOF for that loop to end.. the input stream gets cleared.. i need to get my stream cleared and the next thing is the user's input..

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Something like this?

    Code:
    /* start timer here */
    for (x[y] = getchar(); x[y] != '\n' || timer != MAXSECONDS; y++)
    ;
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Babkockdood, in addition to discarding any input while the timer is active, that code will also eat the first character of input after the timer expires.
    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. Loop reading input
    By c_newbie in forum C Programming
    Replies: 5
    Last Post: 12-08-2010, 09:09 AM
  2. fputs doesn' t stop reading when \0 is found?
    By brack in forum C Programming
    Replies: 5
    Last Post: 09-03-2010, 10:43 AM
  3. Reading in a string from an input file?
    By matthayzon89 in forum C Programming
    Replies: 5
    Last Post: 08-31-2010, 02:14 PM
  4. Reading input and removing characters
    By comp1911 in forum C Programming
    Replies: 5
    Last Post: 04-26-2010, 01:32 AM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM