check if cin ready to read

How can i check cin or scanf is ready to read?

There is a method in Java for this (java.io.BufferedReader.ready()) but I couldn't find any method or function in c++.
How can i check cin or scanf is ready to read?
What do you mean by ready to read?

Absence of stream errors which will not let you read?
Emptyness/not emptyness of input buffer?

Why would you need it?
I want to check Emptyness/not emptyness of input buffer;

When i try to read a something from stdin with cin it waits for user to write. I have to do some process when waiting for user. This is what i want to do.

1
2
3
4
5
6
7
8
9
10

while (true) {
    if (cin is ready to read) {
        cin >> x;
        break;
    }

    // do something;
}

Routines to check to see if standard input has input waiting, with timeout:
http://www.cplusplus.com/forum/beginner/131352/#msg708089

Hope this helps.
1
2
3
4
5
6
while (true) {
    if (cin is ready to read) {
        cin >> x;
        break;
    }
}
This is not going to work anyway. User is not prompted to enter anything until it's requested.

Additionally you will almost always have something in input buffer, namely '\n' character which is not extracted by >>

You can try to use Duoas approach or simly run input request in another thread (this will block console for output too)
Last edited on
Topic archived. No new replies allowed.