2

A followup to this question.

How can I clear the input buffer?

sleep(2);
// clear cin
getchar();

I only want the character that was typed in last, I want to discard any input that was put in while the program was asleep. Is there a way to do that?

Also, I don't want to wait actively to clear cin, thus I can't use ignore().

My approach would have been to get the current size of the buffer and if it's not equal to 0, silently empty the buffer. However, I have not found a way to get the size of the buffer. std::cin.rdbuf()->in_avail() always returns 0 for me. peek() also waits actively.

I don't want to use ncurses.

3
  • So the sleep finishes and before the getchar starts the use types a character in. Does that one count? Commented Jan 20, 2014 at 11:44
  • That's a case I have not yet considered, but is not important for this question for now. Commented Jan 20, 2014 at 11:47
  • @EdHeal: Personally I'd consider that case and "during the sleep" as practically indistinguishable, particularly since a sleep(2) is for "2 seconds or more". Commented Jan 20, 2014 at 13:02

1 Answer 1

1

Having a system supporting tcgetattr/tcsetattr:

#include <iostream>
#include <stdexcept>

#include <termios.h>
#include <unistd.h>

class StdInput
{
    // Constants
    // =========

    public:
    enum {
        Blocking = 0x01,
        Echo = 0x02
    };

    // Static
    // ======

    public:
    static void clear() {
        termios attributes = disable_attributes(Blocking);
        while(std::cin)
            std::cin.get();
        std::cin.clear();
        set(attributes);
    }

    // StdInput
    // ========

    public:
    StdInput()
    :   m_restore(get())
    {}

    ~StdInput()
    {
        set(m_restore, false);
    }

    void disable(unsigned flags) { disable_attributes(flags); }
    void disable_blocking() { disable_attributes(Blocking); }
    void restore() { set(m_restore); }

    private:
    static termios get() {
        const int fd = fileno(stdin);
        termios attributes;
        if(tcgetattr(fd, &attributes) < 0) {
            throw std::runtime_error("StdInput");
        }
        return attributes;
    }

    static void set(const termios& attributes, bool exception = true) {
        const int fd = fileno(stdin);
        if(tcsetattr(fd, TCSANOW, &attributes) < 0 && exception) {
            throw std::runtime_error("StdInput");
        }
    }

    static termios disable_attributes(unsigned flags) {
        termios attributes = get();
        termios a = attributes;
        if(flags & Blocking) {
            a.c_lflag &= ~ICANON;
            a.c_cc[VMIN] = 0;
            a.c_cc[VTIME] = 0;
        }
        if(flags & Echo) {
            a.c_lflag &= ~ECHO;
        }
        set(a);
        return attributes;
    }

    termios m_restore;
};


int main()
{
    // Get something to ignore
    std::cout << "Ignore: ";
    std::cin.get();

    // Do something

    StdInput::clear();

    std::cout << " Input: ";
    std::string line;
    std::getline(std::cin, line);
    std::cout << "Output: " << line << std::endl;

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.